6.8. Sorting Arrays

 
[Page 191 ( continued )]

6.9. The Arrays Class

The java.util.Arrays class contains various static methods for sorting and searching arrays, comparing arrays, and filling array elements. These methods are overloaded for all primitive types.


[Page 192]

You can use the sort method to sort a whole array or a partial array. For example, the following code sorts an array of numbers and an array of characters :

   double   [] numbers = {   6.0   ,   4.4   ,   1.9   ,   2.9   ,   3.4   ,   3.5   };  java.util.Arrays.sort(numbers);   // Sort the whole array    char   [] chars = {   'a'   ,   'A'   ,   '4'   ,   'F'   ,   'D'   ,   'P'   };  java.util.Arrays.sort(chars,   1   ,   3   );   // Sort part of the array  

Invoking sort(numbers) sorts the whole array numbers . Invoking sort(chars, 1, 3) sorts a partial array from chars[1] to chars[ 3-1 ] .

You can use the binarySearch method to search for a key in an array. The array must be pre-sorted in increasing order. If the key is not in the array, the method returns -(insertion point +1) . For example, the following code searches the keys in an array of integers and an array of characters:

   int   [] list = {   2   ,   4   ,   7   ,   10   ,   11   ,   45   ,   50   ,   59   ,   60   ,   66   ,   69   ,   70   ,   79   }; System.out.println(   "(1) Index is "   +  java.util.Arrays.binarySearch(list,   11   ));  System.out.println(   "(2) Index is "   +   java.util.Arrays.binarySearch(list,   12   ));   char   [] chars = {   'a'   ,   'c'   ,   'g'   ,   'x'   ,   'y'   ,   'z'   }; System.out.println(   "(3) Index is "   +   java.util.Arrays.binarySearch(chars,   'a'   )); System.out.println(   "(4) Index is "   +   java.util.Arrays.binarySearch(chars,   't'   )); 

The output of the preceding code is

(1) Index is 4

(2) Index is -6

(3) Index is 0

(4) Index is -4

You can use the equals method to check whether two arrays are equal. Two arrays are equal if they have the same contents. In the following code, list1 and list2 are equal, but list2 and list3 are not:

   int   [] list1 = {   2   ,   4   ,   7   ,   10   };   int   [] list2 = {   2   ,   4   ,   7   ,   10   };   int   [] list3 = {   4   ,   2   ,   7   ,   10   }; System.out.println(  java.util.Arrays.equals(list1, list2)  );  // true  System.out.println(  java.util.Arrays.equals(list2, list3)  );  // false  

You can use the fill method to fill in the whole array or part of the array. For example, the following code fills list1 with 5 and fills 8 into elements list2[1] and list2[3-1] :

   int   [] list1 = {   2   ,   4   ,   7   ,   10   };   int   [] list2 = {   2   ,   4   ,   7   ,   10   };  java.util.Arrays.fill(list1,   5   )  ;  // fill 5 to the whole array   java.util.Arrays.fill(list2,   1   ,   3   ,   8   )  ;  // fill 8 to a partial array  

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net