public class Arrays extends Object
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.
Comparable
,
Comparator
Modifier and Type | Method and Description |
---|---|
static List |
asList(Object[] a)
Returns a list "view" of the specified array.
|
static int |
binarySearch(byte[] a,
byte key)
Perform a binary search of a byte array for a key.
|
static int |
binarySearch(char[] a,
char key)
Perform a binary search of a char array for a key.
|
static int |
binarySearch(double[] a,
double key)
Perform a binary search of a double array for a key.
|
static int |
binarySearch(float[] a,
float key)
Perform a binary search of a float array for a key.
|
static int |
binarySearch(int[] a,
int key)
Perform a binary search of an int array for a key.
|
static int |
binarySearch(long[] a,
long key)
Perform a binary search of a long array for a key.
|
static int |
binarySearch(Object[] a,
Object key)
Perform a binary search of an Object array for a key, using the natural
ordering of the elements.
|
static int |
binarySearch(Object[] a,
Object key,
Comparator c)
Perform a binary search of an Object array for a key, using a supplied
Comparator.
|
static int |
binarySearch(short[] a,
short key)
Perform a binary search of a short array for a key.
|
static boolean |
equals(boolean[] a1,
boolean[] a2)
Compare two boolean arrays for equality.
|
static boolean |
equals(byte[] a1,
byte[] a2)
Compare two byte arrays for equality.
|
static boolean |
equals(char[] a1,
char[] a2)
Compare two char arrays for equality.
|
static boolean |
equals(double[] a1,
double[] a2)
Compare two double arrays for equality.
|
static boolean |
equals(float[] a1,
float[] a2)
Compare two float arrays for equality.
|
static boolean |
equals(int[] a1,
int[] a2)
Compare two int arrays for equality.
|
static boolean |
equals(long[] a1,
long[] a2)
Compare two long arrays for equality.
|
static boolean |
equals(Object[] a1,
Object[] a2)
Compare two Object arrays for equality.
|
static boolean |
equals(short[] a1,
short[] a2)
Compare two short arrays for equality.
|
static void |
fill(boolean[] a,
boolean val)
Fill an array with a boolean value.
|
static void |
fill(boolean[] a,
int fromIndex,
int toIndex,
boolean val)
Fill a range of an array with a boolean value.
|
static void |
fill(byte[] a,
byte val)
Fill an array with a byte value.
|
static void |
fill(byte[] a,
int fromIndex,
int toIndex,
byte val)
Fill a range of an array with a byte value.
|
static void |
fill(char[] a,
char val)
Fill an array with a char value.
|
static void |
fill(char[] a,
int fromIndex,
int toIndex,
char val)
Fill a range of an array with a char value.
|
static void |
fill(double[] a,
double val)
Fill an array with a double value.
|
static void |
fill(double[] a,
int fromIndex,
int toIndex,
double val)
Fill a range of an array with a double value.
|
static void |
fill(float[] a,
float val)
Fill an array with a float value.
|
static void |
fill(float[] a,
int fromIndex,
int toIndex,
float val)
Fill a range of an array with a float value.
|
static void |
fill(int[] a,
int val)
Fill an array with an int value.
|
static void |
fill(int[] a,
int fromIndex,
int toIndex,
int val)
Fill a range of an array with an int value.
|
static void |
fill(long[] a,
int fromIndex,
int toIndex,
long val)
Fill a range of an array with a long value.
|
static void |
fill(long[] a,
long val)
Fill an array with a long value.
|
static void |
fill(Object[] a,
int fromIndex,
int toIndex,
Object val)
Fill a range of an array with an Object value.
|
static void |
fill(Object[] a,
Object val)
Fill an array with an Object value.
|
static void |
fill(short[] a,
int fromIndex,
int toIndex,
short val)
Fill a range of an array with a short value.
|
static void |
fill(short[] a,
short val)
Fill an array with a short value.
|
static void |
sort(byte[] a)
Performs a stable sort on the elements, arranging them according to their
natural order.
|
static void |
sort(byte[] a,
int fromIndex,
int toIndex)
Performs a stable sort on the elements, arranging them according to their
natural order.
|
static void |
sort(char[] a)
Performs a stable sort on the elements, arranging them according to their
natural order.
|
static void |
sort(char[] a,
int fromIndex,
int toIndex)
Performs a stable sort on the elements, arranging them according to their
natural order.
|
static void |
sort(double[] a)
Performs a stable sort on the elements, arranging them according to their
natural order.
|
static void |
sort(double[] a,
int fromIndex,
int toIndex)
Performs a stable sort on the elements, arranging them according to their
natural order.
|
static void |
sort(float[] a)
Performs a stable sort on the elements, arranging them according to their
natural order.
|
static void |
sort(float[] a,
int fromIndex,
int toIndex)
Performs a stable sort on the elements, arranging them according to their
natural order.
|
static void |
sort(int[] a)
Performs a stable sort on the elements, arranging them according to their
natural order.
|
static void |
sort(int[] a,
int fromIndex,
int toIndex)
Performs a stable sort on the elements, arranging them according to their
natural order.
|
static void |
sort(long[] a)
Performs a stable sort on the elements, arranging them according to their
natural order.
|
static void |
sort(long[] a,
int fromIndex,
int toIndex)
Performs a stable sort on the elements, arranging them according to their
natural order.
|
static void |
sort(Object[] a)
Sort an array of Objects according to their natural ordering.
|
static void |
sort(Object[] a,
Comparator c)
Sort an array of Objects according to a Comparator.
|
static void |
sort(Object[] a,
int fromIndex,
int toIndex)
Sort an array of Objects according to their natural ordering.
|
static void |
sort(Object[] a,
int fromIndex,
int toIndex,
Comparator c)
Sort an array of Objects according to a Comparator.
|
static void |
sort(short[] a)
Performs a stable sort on the elements, arranging them according to their
natural order.
|
static void |
sort(short[] a,
int fromIndex,
int toIndex)
Performs a stable sort on the elements, arranging them according to their
natural order.
|
public static int binarySearch(byte[] a, byte key)
a
- the array to search (must be sorted)key
- the value to search forpublic static int binarySearch(char[] a, char key)
a
- the array to search (must be sorted)key
- the value to search forpublic static int binarySearch(short[] a, short key)
a
- the array to search (must be sorted)key
- the value to search forpublic static int binarySearch(int[] a, int key)
a
- the array to search (must be sorted)key
- the value to search forpublic static int binarySearch(long[] a, long key)
a
- the array to search (must be sorted)key
- the value to search forpublic static int binarySearch(float[] a, float key)
a
- the array to search (must be sorted)key
- the value to search forpublic static int binarySearch(double[] a, double key)
a
- the array to search (must be sorted)key
- the value to search forpublic static int binarySearch(Object[] a, Object key)
a
- the array to search (must be sorted)key
- the value to search forClassCastException
- if key could not be compared with one of the
elements of aNullPointerException
- if a null element in a is comparedpublic static int binarySearch(Object[] a, Object key, Comparator c)
a
- the array to search (must be sorted)key
- the value to search forc
- the comparator by which the array is sorted; or null to
use the elements' natural orderClassCastException
- if key could not be compared with one of the
elements of aNullPointerException
- if a null element is compared with natural
ordering (only possible when c is null)public static boolean equals(boolean[] a1, boolean[] a2)
a1
- the first array to comparea2
- the second array to comparepublic static boolean equals(byte[] a1, byte[] a2)
a1
- the first array to comparea2
- the second array to comparepublic static boolean equals(char[] a1, char[] a2)
a1
- the first array to comparea2
- the second array to comparepublic static boolean equals(short[] a1, short[] a2)
a1
- the first array to comparea2
- the second array to comparepublic static boolean equals(int[] a1, int[] a2)
a1
- the first array to comparea2
- the second array to comparepublic static boolean equals(long[] a1, long[] a2)
a1
- the first array to comparea2
- the second array to comparepublic static boolean equals(float[] a1, float[] a2)
a1
- the first array to comparea2
- the second array to comparepublic static boolean equals(double[] a1, double[] a2)
a1
- the first array to comparea2
- the second array to comparepublic static boolean equals(Object[] a1, Object[] a2)
a1
- the first array to comparea2
- the second array to comparepublic static void fill(boolean[] a, boolean val)
a
- the array to fillval
- the value to fill it withpublic static void fill(boolean[] a, int fromIndex, int toIndex, boolean val)
a
- the array to fillfromIndex
- the index to fill from, inclusivetoIndex
- the index to fill to, exclusiveval
- the value to fill withIllegalArgumentException
- if fromIndex > toIndexArrayIndexOutOfBoundsException
- if fromIndex < 0
|| toIndex > a.lengthpublic static void fill(byte[] a, byte val)
a
- the array to fillval
- the value to fill it withpublic static void fill(byte[] a, int fromIndex, int toIndex, byte val)
a
- the array to fillfromIndex
- the index to fill from, inclusivetoIndex
- the index to fill to, exclusiveval
- the value to fill withIllegalArgumentException
- if fromIndex > toIndexArrayIndexOutOfBoundsException
- if fromIndex < 0
|| toIndex > a.lengthpublic static void fill(char[] a, char val)
a
- the array to fillval
- the value to fill it withpublic static void fill(char[] a, int fromIndex, int toIndex, char val)
a
- the array to fillfromIndex
- the index to fill from, inclusivetoIndex
- the index to fill to, exclusiveval
- the value to fill withIllegalArgumentException
- if fromIndex > toIndexArrayIndexOutOfBoundsException
- if fromIndex < 0
|| toIndex > a.lengthpublic static void fill(short[] a, short val)
a
- the array to fillval
- the value to fill it withpublic static void fill(short[] a, int fromIndex, int toIndex, short val)
a
- the array to fillfromIndex
- the index to fill from, inclusivetoIndex
- the index to fill to, exclusiveval
- the value to fill withIllegalArgumentException
- if fromIndex > toIndexArrayIndexOutOfBoundsException
- if fromIndex < 0
|| toIndex > a.lengthpublic static void fill(int[] a, int val)
a
- the array to fillval
- the value to fill it withpublic static void fill(int[] a, int fromIndex, int toIndex, int val)
a
- the array to fillfromIndex
- the index to fill from, inclusivetoIndex
- the index to fill to, exclusiveval
- the value to fill withIllegalArgumentException
- if fromIndex > toIndexArrayIndexOutOfBoundsException
- if fromIndex < 0
|| toIndex > a.lengthpublic static void fill(long[] a, long val)
a
- the array to fillval
- the value to fill it withpublic static void fill(long[] a, int fromIndex, int toIndex, long val)
a
- the array to fillfromIndex
- the index to fill from, inclusivetoIndex
- the index to fill to, exclusiveval
- the value to fill withIllegalArgumentException
- if fromIndex > toIndexArrayIndexOutOfBoundsException
- if fromIndex < 0
|| toIndex > a.lengthpublic static void fill(float[] a, float val)
a
- the array to fillval
- the value to fill it withpublic static void fill(float[] a, int fromIndex, int toIndex, float val)
a
- the array to fillfromIndex
- the index to fill from, inclusivetoIndex
- the index to fill to, exclusiveval
- the value to fill withIllegalArgumentException
- if fromIndex > toIndexArrayIndexOutOfBoundsException
- if fromIndex < 0
|| toIndex > a.lengthpublic static void fill(double[] a, double val)
a
- the array to fillval
- the value to fill it withpublic static void fill(double[] a, int fromIndex, int toIndex, double val)
a
- the array to fillfromIndex
- the index to fill from, inclusivetoIndex
- the index to fill to, exclusiveval
- the value to fill withIllegalArgumentException
- if fromIndex > toIndexArrayIndexOutOfBoundsException
- if fromIndex < 0
|| toIndex > a.lengthpublic static void fill(Object[] a, Object val)
a
- the array to fillval
- the value to fill it withClassCastException
- if val is not an instance of the element
type of a.public static void fill(Object[] a, int fromIndex, int toIndex, Object val)
a
- the array to fillfromIndex
- the index to fill from, inclusivetoIndex
- the index to fill to, exclusiveval
- the value to fill withClassCastException
- if val is not an instance of the element
type of a.IllegalArgumentException
- if fromIndex > toIndexArrayIndexOutOfBoundsException
- if fromIndex < 0
|| toIndex > a.lengthpublic static void sort(byte[] a)
a
- the byte array to sortpublic static void sort(byte[] a, int fromIndex, int toIndex)
a
- the byte array to sortfromIndex
- the first index to sort (inclusive)toIndex
- the last index to sort (exclusive)IllegalArgumentException
- if fromIndex > toIndexArrayIndexOutOfBoundsException
- if fromIndex < 0
|| toIndex > a.lengthpublic static void sort(char[] a)
a
- the char array to sortpublic static void sort(char[] a, int fromIndex, int toIndex)
a
- the char array to sortfromIndex
- the first index to sort (inclusive)toIndex
- the last index to sort (exclusive)IllegalArgumentException
- if fromIndex > toIndexArrayIndexOutOfBoundsException
- if fromIndex < 0
|| toIndex > a.lengthpublic static void sort(short[] a)
a
- the short array to sortpublic static void sort(short[] a, int fromIndex, int toIndex)
a
- the short array to sortfromIndex
- the first index to sort (inclusive)toIndex
- the last index to sort (exclusive)IllegalArgumentException
- if fromIndex > toIndexArrayIndexOutOfBoundsException
- if fromIndex < 0
|| toIndex > a.lengthpublic static void sort(int[] a)
a
- the int array to sortpublic static void sort(int[] a, int fromIndex, int toIndex)
a
- the int array to sortfromIndex
- the first index to sort (inclusive)toIndex
- the last index to sort (exclusive)IllegalArgumentException
- if fromIndex > toIndexArrayIndexOutOfBoundsException
- if fromIndex < 0
|| toIndex > a.lengthpublic static void sort(long[] a)
a
- the long array to sortpublic static void sort(long[] a, int fromIndex, int toIndex)
a
- the long array to sortfromIndex
- the first index to sort (inclusive)toIndex
- the last index to sort (exclusive)IllegalArgumentException
- if fromIndex > toIndexArrayIndexOutOfBoundsException
- if fromIndex < 0
|| toIndex > a.lengthpublic static void sort(float[] a)
a
- the float array to sortpublic static void sort(float[] a, int fromIndex, int toIndex)
a
- the float array to sortfromIndex
- the first index to sort (inclusive)toIndex
- the last index to sort (exclusive)IllegalArgumentException
- if fromIndex > toIndexArrayIndexOutOfBoundsException
- if fromIndex < 0
|| toIndex > a.lengthpublic static void sort(double[] a)
a
- the double array to sortpublic static void sort(double[] a, int fromIndex, int toIndex)
a
- the double array to sortfromIndex
- the first index to sort (inclusive)toIndex
- the last index to sort (exclusive)IllegalArgumentException
- if fromIndex > toIndexArrayIndexOutOfBoundsException
- if fromIndex < 0
|| toIndex > a.lengthpublic static void sort(Object[] a)
a
- the array to be sortedClassCastException
- if any two elements are not mutually
comparableNullPointerException
- if an element is null (since
null.compareTo cannot work)Comparable
public static void sort(Object[] a, Comparator c)
a
- the array to be sortedc
- a Comparator to use in sorting the array; or null to indicate
the elements' natural orderClassCastException
- if any two elements are not mutually
comparable by the Comparator providedNullPointerException
- if a null element is compared with natural
ordering (only possible when c is null)public static void sort(Object[] a, int fromIndex, int toIndex)
a
- the array to be sortedfromIndex
- the index of the first element to be sortedtoIndex
- the index of the last element to be sorted plus oneClassCastException
- if any two elements are not mutually
comparableNullPointerException
- if an element is null (since
null.compareTo cannot work)ArrayIndexOutOfBoundsException
- if fromIndex and toIndex
are not in range.IllegalArgumentException
- if fromIndex > toIndexpublic static void sort(Object[] a, int fromIndex, int toIndex, Comparator c)
a
- the array to be sortedfromIndex
- the index of the first element to be sortedtoIndex
- the index of the last element to be sorted plus onec
- a Comparator to use in sorting the array; or null to indicate
the elements' natural orderClassCastException
- if any two elements are not mutually
comparable by the Comparator providedArrayIndexOutOfBoundsException
- if fromIndex and toIndex
are not in range.IllegalArgumentException
- if fromIndex > toIndexNullPointerException
- if a null element is compared with natural
ordering (only possible when c is null)public static List asList(Object[] a)
a
- the array to return a view ofSerializable
,
RandomAccess
,
Arrays.ArrayList