Index (Frames) | Index (No Frames) | Package | Package Tree | Tree
java.util

Class Arrays

java.lang.Object
|
+--java.util.Arrays


public class Arrays

extends Object

This class contains various static utility methods performing operations on arrays, and a method to provide a List "view" of an array to facilitate using arrays with Collection-based APIs. All methods throw a NullPointerException if the parameter array is null.

Implementations may use their own algorithms, but must obey the general properties; for example, the sort must be stable and n*log(n) complexity. Sun's implementation of sort, and therefore ours, is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.

Since:Authors:See Also:

Method Summary

static java.util.ListasList(final Object[][] a)

Returns a list "view" of the specified array.
static intbinarySearch(byte[] a, byte key)

Perform a binary search of a byte array for a key.
static intbinarySearch(char[] a, char key)

Perform a binary search of a char array for a key.
static intbinarySearch(short[] a, short key)

Perform a binary search of a short array for a key.
static intbinarySearch(int[] a, int key)

Perform a binary search of an int array for a key.
static intbinarySearch(long[] a, long key)

Perform a binary search of a long array for a key.
static intbinarySearch(float[] a, float key)

Perform a binary search of a float array for a key.
static intbinarySearch(double[] a, double key)

Perform a binary search of a double array for a key.
static intbinarySearch(java.lang.Object[] a, java.lang.Object key)

Perform a binary search of an Object array for a key, using the natural ordering of the elements.
static intbinarySearch(java.lang.Object[] a, java.lang.Object key, java.util.Comparator c)

Perform a binary search of an Object array for a key, using a supplied Comparator.
static booleanequals(boolean[] a1, boolean[] a2)

Compare two boolean arrays for equality.
static booleanequals(byte[] a1, byte[] a2)

Compare two byte arrays for equality.
static booleanequals(char[] a1, char[] a2)

Compare two char arrays for equality.
static booleanequals(short[] a1, short[] a2)

Compare two short arrays for equality.
static booleanequals(int[] a1, int[] a2)

Compare two int arrays for equality.
static booleanequals(long[] a1, long[] a2)

Compare two long arrays for equality.
static booleanequals(float[] a1, float[] a2)

Compare two float arrays for equality.
static booleanequals(double[] a1, double[] a2)

Compare two double arrays for equality.
static booleanequals(java.lang.Object[] a1, java.lang.Object[] a2)

Compare two Object arrays for equality.
static voidfill(boolean[] a, boolean val)

Fill an array with a boolean value.
static voidfill(boolean[] a, int fromIndex, int toIndex, boolean val)

Fill a range of an array with a boolean value.
static voidfill(byte[] a, byte val)

Fill an array with a byte value.
static voidfill(byte[] a, int fromIndex, int toIndex, byte val)

Fill a range of an array with a byte value.
static voidfill(char[] a, char val)

Fill an array with a char value.
static voidfill(char[] a, int fromIndex, int toIndex, char val)

Fill a range of an array with a char value.
static voidfill(short[] a, short val)

Fill an array with a short value.
static voidfill(short[] a, int fromIndex, int toIndex, short val)

Fill a range of an array with a short value.
static voidfill(int[] a, int val)

Fill an array with an int value.
static voidfill(int[] a, int fromIndex, int toIndex, int val)

Fill a range of an array with an int value.
static voidfill(long[] a, long val)

Fill an array with a long value.
static voidfill(long[] a, int fromIndex, int toIndex, long val)

Fill a range of an array with a long value.
static voidfill(float[] a, float val)

Fill an array with a float value.
static voidfill(float[] a, int fromIndex, int toIndex, float val)

Fill a range of an array with a float value.
static voidfill(double[] a, double val)

Fill an array with a double value.
static voidfill(double[] a, int fromIndex, int toIndex, double val)

Fill a range of an array with a double value.
static voidfill(java.lang.Object[] a, java.lang.Object val)

Fill an array with an Object value.
static voidfill(java.lang.Object[] a, int fromIndex, int toIndex, java.lang.Object val)

Fill a range of an array with an Object value.
static voidsort(byte[] a)

Performs a stable sort on the elements, arranging them according to their natural order.
static voidsort(byte[] a, int fromIndex, int toIndex)

Performs a stable sort on the elements, arranging them according to their natural order.
static voidsort(char[] a)

Performs a stable sort on the elements, arranging them according to their natural order.
static voidsort(char[] a, int fromIndex, int toIndex)

Performs a stable sort on the elements, arranging them according to their natural order.
static voidsort(short[] a)

Performs a stable sort on the elements, arranging them according to their natural order.
static voidsort(short[] a, int fromIndex, int toIndex)

Performs a stable sort on the elements, arranging them according to their natural order.
static voidsort(int[] a)

Performs a stable sort on the elements, arranging them according to their natural order.
static voidsort(int[] a, int fromIndex, int toIndex)

Performs a stable sort on the elements, arranging them according to their natural order.
static voidsort(long[] a)

Performs a stable sort on the elements, arranging them according to their natural order.
static voidsort(long[] a, int fromIndex, int toIndex)

Performs a stable sort on the elements, arranging them according to their natural order.
static voidsort(float[] a)

Performs a stable sort on the elements, arranging them according to their natural order.
static voidsort(float[] a, int fromIndex, int toIndex)

Performs a stable sort on the elements, arranging them according to their natural order.
static voidsort(double[] a)

Performs a stable sort on the elements, arranging them according to their natural order.
static voidsort(double[] a, int fromIndex, int toIndex)

Performs a stable sort on the elements, arranging them according to their natural order.
static voidsort(java.lang.Object[] a)

Sort an array of Objects according to their natural ordering.
static voidsort(java.lang.Object[] a, java.util.Comparator c)

Sort an array of Objects according to a Comparator.
static voidsort(java.lang.Object[] a, int fromIndex, int toIndex)

Sort an array of Objects according to their natural ordering.
static voidsort(java.lang.Object[] a, int fromIndex, int toIndex, java.util.Comparator c)

Sort an array of Objects according to a Comparator.

Method Details

asList

public static List asList(final Object[][] a)

Returns a list "view" of the specified array. This method is intended to make it easy to use the Collections API with existing array-based APIs and programs. Changes in the list or the array show up in both places. The list does not support element addition or removal, but does permit value modification. The returned list implements both Serializable and RandomAccess.

Parameters:

Returns:

See Also:


binarySearch

public static int binarySearch(byte[] a, byte key)

Perform a binary search of a byte array for a key. The array must be sorted (as by the sort() method) - if it is not, the behaviour of this method is undefined, and may be an infinite loop. If the array contains the key more than once, any one of them may be found. Note: although the specification allows for an infinite loop if the array is unsorted, it will not happen in this implementation.

Parameters:

Returns:


binarySearch

public static int binarySearch(char[] a, char key)

Perform a binary search of a char array for a key. The array must be sorted (as by the sort() method) - if it is not, the behaviour of this method is undefined, and may be an infinite loop. If the array contains the key more than once, any one of them may be found. Note: although the specification allows for an infinite loop if the array is unsorted, it will not happen in this implementation.

Parameters:

Returns:


binarySearch

public static int binarySearch(double[] a, double key)

Perform a binary search of a double array for a key. The array must be sorted (as by the sort() method) - if it is not, the behaviour of this method is undefined, and may be an infinite loop. If the array contains the key more than once, any one of them may be found. Note: although the specification allows for an infinite loop if the array is unsorted, it will not happen in this implementation.

Parameters:

Returns:


binarySearch

public static int binarySearch(float[] a, float key)

Perform a binary search of a float array for a key. The array must be sorted (as by the sort() method) - if it is not, the behaviour of this method is undefined, and may be an infinite loop. If the array contains the key more than once, any one of them may be found. Note: although the specification allows for an infinite loop if the array is unsorted, it will not happen in this implementation.

Parameters:

Returns:


binarySearch

public static int binarySearch(int[] a, int key)

Perform a binary search of an int array for a key. The array must be sorted (as by the sort() method) - if it is not, the behaviour of this method is undefined, and may be an infinite loop. If the array contains the key more than once, any one of them may be found. Note: although the specification allows for an infinite loop if the array is unsorted, it will not happen in this implementation.

Parameters:

Returns:


binarySearch

public static int binarySearch(java.lang.Object[] a, java.lang.Object key)

Perform a binary search of an Object array for a key, using the natural ordering of the elements. The array must be sorted (as by the sort() method) - if it is not, the behaviour of this method is undefined, and may be an infinite loop. Further, the key must be comparable with every item in the array. If the array contains the key more than once, any one of them may be found. Note: although the specification allows for an infinite loop if the array is unsorted, it will not happen in this (JCL) implementation.

Parameters:

Returns:

Throws:


binarySearch

public static int binarySearch(java.lang.Object[] a, java.lang.Object key, java.util.Comparator c)

Perform a binary search of an Object array for a key, using a supplied Comparator. The array must be sorted (as by the sort() method with the same Comparator) - if it is not, the behaviour of this method is undefined, and may be an infinite loop. Further, the key must be comparable with every item in the array. If the array contains the key more than once, any one of them may be found. Note: although the specification allows for an infinite loop if the array is unsorted, it will not happen in this (JCL) implementation.

Parameters:

Returns:

Throws:


binarySearch

public static int binarySearch(long[] a, long key)

Perform a binary search of a long array for a key. The array must be sorted (as by the sort() method) - if it is not, the behaviour of this method is undefined, and may be an infinite loop. If the array contains the key more than once, any one of them may be found. Note: although the specification allows for an infinite loop if the array is unsorted, it will not happen in this implementation.

Parameters:

Returns:


binarySearch

public static int binarySearch(short[] a, short key)

Perform a binary search of a short array for a key. The array must be sorted (as by the sort() method) - if it is not, the behaviour of this method is undefined, and may be an infinite loop. If the array contains the key more than once, any one of them may be found. Note: although the specification allows for an infinite loop if the array is unsorted, it will not happen in this implementation.

Parameters:

Returns:


equals

public static boolean equals(boolean[] a1, boolean[] a2)

Compare two boolean arrays for equality.

Parameters:

Returns:


equals

public static boolean equals(byte[] a1, byte[] a2)

Compare two byte arrays for equality.

Parameters:

Returns:


equals

public static boolean equals(char[] a1, char[] a2)

Compare two char arrays for equality.

Parameters:

Returns:


equals

public static boolean equals(double[] a1, double[] a2)

Compare two double arrays for equality.

Parameters:

Returns:


equals

public static boolean equals(float[] a1, float[] a2)

Compare two float arrays for equality.

Parameters:

Returns:


equals

public static boolean equals(int[] a1, int[] a2)

Compare two int arrays for equality.

Parameters:

Returns:


equals

public static boolean equals(java.lang.Object[] a1, java.lang.Object[] a2)

Compare two Object arrays for equality.

Parameters:

Returns:


equals

public static boolean equals(long[] a1, long[] a2)

Compare two long arrays for equality.

Parameters:

Returns:


equals

public static boolean equals(short[] a1, short[] a2)

Compare two short arrays for equality.

Parameters:

Returns:


fill

public static void fill(boolean[] a, boolean val)

Fill an array with a boolean value.

Parameters:


fill

public static void fill(boolean[] a, int fromIndex, int toIndex, boolean val)

Fill a range of an array with a boolean value.

Parameters:

Throws:


fill

public static void fill(byte[] a, byte val)

Fill an array with a byte value.

Parameters:


fill

public static void fill(byte[] a, int fromIndex, int toIndex, byte val)

Fill a range of an array with a byte value.

Parameters:

Throws:


fill

public static void fill(char[] a, char val)

Fill an array with a char value.

Parameters:


fill

public static void fill(char[] a, int fromIndex, int toIndex, char val)

Fill a range of an array with a char value.

Parameters:

Throws:


fill

public static void fill(double[] a, double val)

Fill an array with a double value.

Parameters:


fill

public static void fill(double[] a, int fromIndex, int toIndex, double val)

Fill a range of an array with a double value.

Parameters:

Throws:


fill

public static void fill(float[] a, float val)

Fill an array with a float value.

Parameters:


fill

public static void fill(float[] a, int fromIndex, int toIndex, float val)

Fill a range of an array with a float value.

Parameters:

Throws:


fill

public static void fill(int[] a, int val)

Fill an array with an int value.

Parameters:


fill

public static void fill(int[] a, int fromIndex, int toIndex, int val)

Fill a range of an array with an int value.

Parameters:

Throws:


fill

public static void fill(java.lang.Object[] a, int fromIndex, int toIndex, java.lang.Object val)

Fill a range of an array with an Object value.

Parameters:

Throws:


fill

public static void fill(java.lang.Object[] a, java.lang.Object val)

Fill an array with an Object value.

Parameters:

Throws:


fill

public static void fill(long[] a, int fromIndex, int toIndex, long val)

Fill a range of an array with a long value.

Parameters:

Throws:


fill

public static void fill(long[] a, long val)

Fill an array with a long value.

Parameters:


fill

public static void fill(short[] a, int fromIndex, int toIndex, short val)

Fill a range of an array with a short value.

Parameters:

Throws:


fill

public static void fill(short[] a, short val)

Fill an array with a short value.

Parameters:


sort

public static void sort(byte[] a)

Performs a stable sort on the elements, arranging them according to their natural order.

Parameters:


sort

public static void sort(byte[] a, int fromIndex, int toIndex)

Performs a stable sort on the elements, arranging them according to their natural order.

Parameters:

Throws:


sort

public static void sort(char[] a)

Performs a stable sort on the elements, arranging them according to their natural order.

Parameters:


sort

public static void sort(char[] a, int fromIndex, int toIndex)

Performs a stable sort on the elements, arranging them according to their natural order.

Parameters:

Throws:


sort

public static void sort(double[] a)

Performs a stable sort on the elements, arranging them according to their natural order.

Parameters:


sort

public static void sort(double[] a, int fromIndex, int toIndex)

Performs a stable sort on the elements, arranging them according to their natural order.

Parameters:

Throws:


sort

public static void sort(float[] a)

Performs a stable sort on the elements, arranging them according to their natural order.

Parameters:


sort

public static void sort(float[] a, int fromIndex, int toIndex)

Performs a stable sort on the elements, arranging them according to their natural order.

Parameters:

Throws:


sort

public static void sort(int[] a)

Performs a stable sort on the elements, arranging them according to their natural order.

Parameters:


sort

public static void sort(int[] a, int fromIndex, int toIndex)

Performs a stable sort on the elements, arranging them according to their natural order.

Parameters:

Throws:


sort

public static void sort(java.lang.Object[] a)

Sort an array of Objects according to their natural ordering. The sort is guaranteed to be stable, that is, equal elements will not be reordered. The sort algorithm is a mergesort with the merge omitted if the last element of one half comes before the first element of the other half. This algorithm gives guaranteed O(n*log(n)) time, at the expense of making a copy of the array.

Parameters:

Throws:

See Also:


sort

public static void sort(java.lang.Object[] a, int fromIndex, int toIndex)

Sort an array of Objects according to their natural ordering. The sort is guaranteed to be stable, that is, equal elements will not be reordered. The sort algorithm is a mergesort with the merge omitted if the last element of one half comes before the first element of the other half. This algorithm gives guaranteed O(n*log(n)) time, at the expense of making a copy of the array.

Parameters:

Throws:


sort

public static void sort(java.lang.Object[] a, int fromIndex, int toIndex, java.util.Comparator c)

Sort an array of Objects according to a Comparator. The sort is guaranteed to be stable, that is, equal elements will not be reordered. The sort algorithm is a mergesort with the merge omitted if the last element of one half comes before the first element of the other half. This algorithm gives guaranteed O(n*log(n)) time, at the expense of making a copy of the array.

Parameters:

Throws:


sort

public static void sort(java.lang.Object[] a, java.util.Comparator c)

Sort an array of Objects according to a Comparator. The sort is guaranteed to be stable, that is, equal elements will not be reordered. The sort algorithm is a mergesort with the merge omitted if the last element of one half comes before the first element of the other half. This algorithm gives guaranteed O(n*log(n)) time, at the expense of making a copy of the array.

Parameters:

Throws:


sort

public static void sort(long[] a)

Performs a stable sort on the elements, arranging them according to their natural order.

Parameters:


sort

public static void sort(long[] a, int fromIndex, int toIndex)

Performs a stable sort on the elements, arranging them according to their natural order.

Parameters:

Throws:


sort

public static void sort(short[] a)

Performs a stable sort on the elements, arranging them according to their natural order.

Parameters:


sort

public static void sort(short[] a, int fromIndex, int toIndex)

Performs a stable sort on the elements, arranging them according to their natural order.

Parameters:

Throws: