If array contains given element : Array Sort Search « Collections Data Structure « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Collections Data Structure » Array Sort SearchScreenshots 
If array contains given element
   
/*
 * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
 
 * Project: OpenSubsystems
 
 * $Id: ArrayUtils.java,v 1.9 2007/01/23 06:00:30 bastafidli Exp $
 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License. 
 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */


/**
 * Collection of useful utilities to work with arrays. 
 
 @version $Id: ArrayUtils.java,v 1.9 2007/01/23 06:00:30 bastafidli Exp $
 @author Peter Satury
 * @code.reviewer Miro Halas
 * @code.reviewed 1.5 2005/07/29 07:36:24 bastafidli
 */
public final class ArrayUtils
{
   /** 
    * Private constructor since this class cannot be instantiated
    */
   private ArrayUtils(
   )
   {
      // Do nothing
   }
   
   // Public methods ///////////////////////////////////////////////////////////

   /**
    * Method to exclude 2 arrays of ints so that the result contains all elements
    * from the first array, which are not in the second array.
    *  
    @param arrBase - base array to exclude from 
    @param arrExclude - array to exclude from the first one
    @return int[] - array which contains all elements from the first array 
    *                 which are not in the second array or null
    */
   public static int[] exclude(
      int[] arrBase, 
      int[] arrExclude
   )
   {
      int[] arrReturn = null;

      if ((arrBase != null&& (arrBase.length > 0&& (arrExclude != null)
         && (arrExclude.length > 0))
      {
         int[] arrHelp;
         int   iCount1;
         int   iHelp;
         int   iLength = 0;
         
         arrHelp = new int[arrBase.length];
         for (iCount1 = 0; iCount1 < arrBase.length; iCount1++)
         {
            iHelp = arrBase[iCount1];
            if (ArrayUtils.contains(arrExclude, iHelp== -1)
            {
               // If the element is not part of the second array then it should
               // be included in the result
               arrHelp[iLength++= iHelp;
            }
         }
         
         // Shrink the array
         // TODO: Performance: Replace this with System.arraycopy
         arrReturn = new int[iLength];
         for (int iCount = 0; iCount < iLength; iCount++)
         {
            arrReturn[iCount= arrHelp[iCount];
         }
      }
      else
      {
         arrReturn = arrBase;
      }
      
      return arrReturn;
   }
   
   /**
    * Test if specified array contains given element and if it does, find 
    * its position.
    
    @param source - array to search, can be null
    @param iTarget - element to find
    @return int - -1 if it doesn't exist there otherwise its position
    */
   public static int contains(
      int[] source,
      int   iTarget
   )
   {
      int iReturn = -1;
      
      if ((source != null&& (source.length > 0))
      {   
         int iIndex;
         
         for (iIndex = 0; iIndex < source.length; iIndex++)
         {
            if (source[iIndex== iTarget)
            {
               iReturn = iIndex;
               break;
            }
         }
      }
      
      return iReturn;
   }

   /**
    * Sum all elements in the array.
    
    @param source - array to sum elements of
    @return long - sum of the elements in the array
    */
   public static long sum(
      int[] source
   )
   {
      int iReturn = 0;
      
      if ((source != null&& (source.length > 0))
      {   
         int iIndex;
         
         for (iIndex = 0; iIndex < source.length; iIndex++)
         {
            iReturn += source[iIndex];
         }
      }
      
      return iReturn;
   }
}

   
    
    
  
Related examples in the same category
1. Quicksort implementation for sorting arrays
2. Search for a specified value of an array
3. Inserting an Element into a Sorted Array
4. How to sort an array
5. Sorting an Array in Descending (Reverse) Order
6. Sort in reverse order
7. Sorting an Array
8. Finding an Element in a Sorted Array
9. Sort array values in descending order
10. Find items in an array
11. Reverses the order of the given object array.
12. Reverses the order of the given long type value array.
13. Reverses the order of the given int type value array.
14. Reverses the order of the given short type value array.
15. Reverses the order of the given char type value array.
16. Reverses the order of the given byte type array.
17. Reverses the order of the given double value type array.
18. Reverses the order of the given float type value array.
19. Reverses the order of the given boolean type value array.
20. Produces a new array containing the elements between the start and end indices.
21. Produces a new long array containing the elements between the start and end indices.
22. Produces a new int array containing the elements between the start and end indices.
23. Produces a new short array containing the elements between the start and end indices.
24. Produces a new char array containing the elements between the start and end indices.
25. Produces a new byte array containing the elements between the start and end indices.
26. Produces a new double array containing the elements between the start and end indices.
27. Produces a new float array containing the elements between the start and end indices.
28. Produces a new boolean array containing the elements between the start and end indices.
29. Get index of long type array
30. Get the index and last index of an int type value array
31. Get the index and last index of a short type value array
32. Get the index and last index of a char type value array
33. Get the index and last index of byte type array
34. Get the index and last index of a double type array
35. Get the element index and last index in a float type value array
36. Get the element index or last index among a boolean type array
37. Finds the index of the given object in the array starting at the given index.
38. Finds the index of the given object in the array.
39. Returns the index in the source array where the first occurrence of the specified byte pattern is found
40. Returns the index in the source array where the first occurrence of the specified text (a String, converted to a byte array) is found
41. Returns the index in the source array where the last occurrence of the specified byte pattern is found
42. Returns the index in the source array where the last occurrence of the specified text (a String, converted to a byte array) is found
43. Returns the maximum value in a byte-type array.
44. Returns the maximum value in a double-type array.
45. Returns the maximum value in a float-type array.
46. Returns the maximum value in a long-value array.
47. Returns the maximum value in a short-type array.
48. Returns the maximum value in an int-type array.
49. Returns the minimum value in a double-type array.
50. Returns the minimum value in a float-value array.
51. Returns the minimum value in a short-type array.
52. Returns the minimum value in an array.
53. Returns the minimum value in an int-type array.
54. Returns the minimum value in an short-type array.
55. Search for an object in an array.
56. Finds the last index of the given object in the array starting at the given index.
57. Finds the last index of the given object within the array.
58. Sort array utilities
59. Check if object 1 contains object 2
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.