Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 688
نمايش فراداده

Arraysjava.util

Java 1.2

This class defines static methods for sorting, searching, and performing other useful operations on arrays. It also defines the asList( ) method, which returns a List wrapper around a specified array of objects. Any changes made to the List are also made to the underlying array. This is a powerful method that allows any array of objects to be manipulated in any of the ways a List can be manipulated. It provides a link between arrays and the Java collections framework.

The various sort( ) methods sort an array (or a specified portion of an array) in place. Variants of the method are defined for arrays of each primitive type and for arrays of Object. For arrays of primitive types, the sorting is done according to the natural ordering of the type. For arrays of objects, the sorting is done according to the specified Comparator, or, if the array contains only java.lang.Comparable objects, according to the ordering defined by that interface. When sorting an array of objects, a stable sorting algorithm is used so that the relative ordering of equal objects is not disturbed. (This allows repeated sorts to order objects by key and subkey, for example.)

The binarySearch( ) methods perform an efficient search (in logarithmic time) of a sorted array for a specified value. If a match is found in the array, binarySearch( ) returns the index of the match. If no match is found, the method returns a negative number. For a negative return value r, the index -(r+1) specifies the array index at which the specified value can be inserted to maintain the sorted order of the array. When the array to be searched is an array of objects, the elements of the array must all implement java.lang.Comparable, or you must provide a Comparator object to compare them.

The equals( ) methods test whether two arrays are equal. Two arrays of primitive type are equal if they contain the same number of elements and if corresponding pairs of elements are equal according to the == operator. Two arrays of objects are equal if they contain the same number of elements and if corresponding pairs of elements are equal according to the equals( ) method defined by those objects. The fill( ) methods fill an array or a specified range of an array with the specified value.

Java 5.0 adds hashCode( ) methods that compute a hashcode for the contents of the array. These methods are compatible with the equals( ) methods: equal( ) arrays will always have the same hashCode( ). Java 5.0 also adds deepEquals( ) and deepHashCode( ) methods that handle multi-dimensional arrays. Finally, the Java 5.0 toString( ) and deepToString( ) methods convert arrays to strings. The returned strings are a comma-separated list of elements enclosed in square brackets.

public class

Arrays { // No Constructor // Public Class Methods public static <T> List<T>

asList (T ...

a ); public static int

binarySearch (char[ ]

a , char

key ); public static int

binarySearch (short[ ]

a , short

key ); public static int

binarySearch (long[ ]

a , long

key ); public static int

binarySearch (int[ ]

a , int

key ); public static int

binarySearch (float[ ]

a , float

key ); public static int

binarySearch (Object[ ]

a , Object

key ); public static int

binarySearch (byte[ ]

a , byte

key ); public static int

binarySearch (double[ ]

a , double

key ); public static <T> int

binarySearch (T[ ]

a , T

key , Comparator<? super T>

c );

5.0 public static boolean

deepEquals (Object[ ]

a1 , Object[ ]

a2 );

5.0 public static int

deepHashCode (Object[ ]

a );

5.0 public static String

deepToString (Object[ ]

a ); public static boolean

equals (boolean[ ]

a , boolean[ ]

a2 ); public static boolean

equals (long[ ]

a , long[ ]

a2 ); public static boolean

equals (float[ ]

a , float[ ]

a2 ); public static boolean

equals (double[ ]

a , double[ ]

a2 ); public static boolean

equals (char[ ]

a , char[ ]

a2 ); public static boolean

equals (byte[ ]

a , byte[ ]

a2 ); public static boolean

equals (int[ ]

a , int[ ]

a2 ); public static boolean

equals (short[ ]

a , short[ ]

a2 ); public static boolean

equals (Object[ ]

a , Object[ ]

a2 ); public static void

fill (char[ ]

a , char

val ); public static void

fill (short[ ]

a , short

val ); public static void

fill (byte[ ]

a , byte

val ); public static void

fill (int[ ]

a , int

val ); public static void

fill (double[ ]

a , double

val ); public static void

fill (boolean[ ]

a , boolean

val ); public static void

fill (Object[ ]

a , Object

val ); public static void

fill (float[ ]

a , float

val ); public static void

fill (long[ ]

a , long

val ); public static void

fill (int[ ]

a , int

fromIndex , int

toIndex , int

val ); public static void

fill (double[ ]

a , int

fromIndex , int

toIndex , double

val ); public static void

fill (short[ ]

a , int

fromIndex , int

toIndex , short

val ); public static void

fill (char[ ]

a , int

fromIndex , int

toIndex , char

val ); public static void

fill (float[ ]

a , int

fromIndex , int

toIndex , float

val ); public static void

fill (byte[ ]

a , int

fromIndex , int

toIndex , byte

val ); public static void

fill (boolean[ ]

a , int

fromIndex , int

toIndex , boolean

val ); public static void

fill (Object[ ]

a , int

fromIndex , int

toIndex , Object

val ); public static void

fill (long[ ]

a , int

fromIndex , int

toIndex , long

val );

5.0 public static int

hashCode (short[ ]

a );

5.0 public static int

hashCode (char[ ]

a );

5.0 public static int

hashCode (long[ ]

a );

5.0 public static int

hashCode (int[ ]

a );

5.0 public static int

hashCode (byte[ ]

a );

5.0 public static int

hashCode (double[ ]

a );

5.0 public static int

hashCode (Object[ ]

a );

5.0 public static int

hashCode (boolean[ ]

a );

5.0 public static int

hashCode (float[ ]

a ); public static void

sort (Object[ ]

a ); public static void

sort (short[ ]

a ); public static void

sort (float[ ]

a ); public static void

sort (double[ ]

a ); public static void

sort (long[ ]

a ); public static void

sort (byte[ ]

a ); public static void

sort (char[ ]

a ); public static void

sort (int[ ]

a ); public static <T> void

sort (T[ ]

a , Comparator<? super T>

c ); public static void

sort (short[ ]

a , int

fromIndex , int

toIndex ); public static void

sort (int[ ]

a , int

fromIndex , int

toIndex ); public static void

sort (char[ ]

a , int

fromIndex , int

toIndex ); public static void

sort (long[ ]

a , int

fromIndex , int

toIndex ); public static void

sort (float[ ]

a , int

fromIndex , int

toIndex ); public static void

sort (double[ ]

a , int

fromIndex , int

toIndex ); public static void

sort (byte[ ]

a , int

fromIndex , int

toIndex ); public static void

sort (Object[ ]

a , int

fromIndex , int

toIndex ); public static <T> void

sort (T[ ]

a , int

fromIndex , int

toIndex , Comparator<? super T>

c );

5.0 public static String

toString (float[ ]

a );

5.0 public static String

toString (boolean[ ]

a );

5.0 public static String

toString (Object[ ]

a );

5.0 public static String

toString (double[ ]

a );

5.0 public static String

toString (int[ ]

a );

5.0 public static String

toString (long[ ]

a );

5.0 public static String

toString (short[ ]

a );

5.0 public static String

toString (byte[ ]

a );

5.0 public static String

toString (char[ ]

a ); }