Java核心类库学习之Arrays类
Arrays类包含许多用于操作数组的方法
-
Arrays所属的包
java.util包 -
Arrays没有任何继承关系,默认继承Object类。
-
Arrays类的构造方法是私有的,不能直接调用创建对象,构造方法代码如下:
// Suppresses default constructor, ensuring non-instantiability.
private Arrays() {}
-
Arrays类的所有方法和属性都是用static修饰的静态的属性和方法,不需要创建对象,用类名直接调用。
-
Arrays类常用方法
注:方法整理时,为了简便一点,"=“前为返回值类型,”="后为方法名和所需参数。
(1) binarySearch()方法
返回给定类型的(值或者对象)在给定类型的数组中的位置,存在很多重载方法,如下所示:
int = binarySearch(byte[] a, byte key)
int = binarySearch(byte[] a, int fromIndex, int toIndex, byte key)
int = binarySearch(char[] a, char key)
int = binarySearch(char[] a, int fromIndex, int toIndex, char key)
int = binarySearch(double[] a, double key)
int = binarySearch(double[] a, int fromIndex, int toIndex, double key)
int = binarySearch(float[] a, float key)
int = binarySearch(float[] a, int fromIndex, int toIndex, float key)
int = binarySearch(int[] a, int key)
int = binarySearch(int[] a, int fromIndex, int toIndex, int key)
int = binarySearch(long[] a, int fromIndex, int toIndex, long key)
int = binarySearch(long[] a, long key)
int = binarySearch(Object[] a, int fromIndex, int toIndex, Object key)
int = binarySearch(Object[] a, Object key)
int = binarySearch(short[] a, int fromIndex, int toIndex, short key)
int = binarySearch(short[] a, short key)
int = binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c)
int = binarySearch(T[] a, T key, Comparator<? super T> c)
(2) copyOf(数组,长度) 复制给定长度的数组,存在很多方法重载,如下所示:
boolean[] = copyOf(boolean[] original, int newLength)
char[] = copyOf(char[] original, int newLength)
double[] = copyOf(double[] original, int newLength)
float[] = copyOf(float[] original, int newLength)
int[] = copyOf(int[] original, int newLength)
long[] = copyOf(long[] original, int newLength)
short[] = copyOf(short[] original, int newLength)
T[] = copyOf(T[] original, int newLength)
<T,U> T[] = copyOf(U[] original, int newLength, 类<? extends T[]> newType)
(3) copyOfRange(数组,起始位置,终止位置),复制给定范围长度的数组,存在很多方法重载,和copyOf()方法的重载一样
boolean[] = copyOfRange(boolean[] original, int from, int to)
byte[] = copyOfRange(byte[] original, int from, int to)
char[] = copyOfRange(char[] original, int from, int to)
double[] = copyOfRange(double[] original, int from, int to)
float[] = copyOfRange(float[] original, int from, int to)
int[] = copyOfRange(int[] original, int from, int to)
long[] = copyOfRange(long[] original, int from, int to)
short[] = copyOfRange(short[] original, int from, int to)
T[] = copyOfRange(T[] original, int from, int to)
<T,U> T[] = copyOfRange(U[] original, int from, int to, 类<? extends T[]> newType)
(4) sort(数组) / sort(数组,起始位置,终止位置) ,将给定的数组升序排序 / 将给定范围长度的数组升序排序,存在很多重载方法如下:
void = sort(byte[] a)
void = sort(byte[] a, int fromIndex, int toIndex)
void = sort(char[] a)
void = sort(char[] a, int fromIndex, int toIndex)
void = sort(double[] a)
void = sort(double[] a, int fromIndex, int toIndex)
void = sort(float[] a)
void = sort(float[] a, int fromIndex, int toIndex)
void = sort(int[] a)
void = sort(int[] a, int fromIndex, int toIndex)
void = sort(long[] a)
void = sort(long[] a, int fromIndex, int toIndex)
void = sort(Object[] a)
void = sort(Object[] a, int fromIndex, int toIndex)
void = sort(short[] a)
void = sort(short[] a, int fromIndex, int toIndex)
void = sort(T[] a, Comparator<? super T> c)
void = sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)
(5)parallelSort(数组) / parallelSort(数组,起始位置,终止位置) ,将给定的数组升序排序 / 将给定范围长度的数组升序排序,重载方法与sort一样,这里不再赘述。
(6)toString(数组) 返回给定数组内容的字符串形式,存在的重载方法如下:
String = toString(boolean[] a)
String = toString(byte[] a)
String = toString(char[] a)
String = toString(double[] a)
String = toString(float[] a)
String = toString(int[] a)
String = toString(long[] a)
String = toString(Object[] a)
String = toString(short[] a)
(7)hashCode(数组) 返回给定数组内容的哈希码,存在的重载方法如下:
int = hashCode(boolean[] a)
int = hashCode(byte[] a)
int = hashCode(char[] a)
int = hashCode(double[] a)
int = hashCode(float[] a)
int = hashCode(int[] a)
int = hashCode(long[] a)
int = hashCode(Object[] a)
int = hashCode(short[] a)
(8)fill(数组,值) / fill(数组 ,起始位置, 终止位置, 值(对象)) ,将给定的值存入数组 / 将给定的值存入给定范围长度的数组,方法重载如下:
void = fill(boolean[] a, boolean val)
void = fill(boolean[] a, int fromIndex, int toIndex, boolean val)
void = fill(byte[] a, byte val)
void = fill(byte[] a, int fromIndex, int toIndex, byte val)
void = fill(char[] a, char val)
void = fill(char[] a, int fromIndex, int toIndex, char val)
void = fill(double[] a, double val)
void = fill(double[] a, int fromIndex, int toIndex, double val)
void = fill(float[] a, float val)
void = fill(float[] a, int fromIndex, int toIndex, float val)
void = fill(int[] a, int val)
void = fill(int[] a, int fromIndex, int toIndex, int val)
void = fill(long[] a, int fromIndex, int toIndex, long val)
void = fill(long[] a, long val)
void = fill(Object[] a, int fromIndex, int toIndex, Object val)
void = fill(Object[] a, Object val)
void = fill(short[] a, int fromIndex, int toIndex, short val)
void = fill(short[] a, short val)
(9) equals(数组1,数组2) 比较给定的两个数组是否相同,相同返回true,不同返回false,存在重载方法如下:
boolean = equals(boolean[] a, boolean[] a2)
boolean = equals(byte[] a, byte[] a2)
boolean = equals(char[] a, char[] a2)
boolean = equals(double[] a, double[] a2)
boolean = equals(float[] a, float[] a2)
boolean = equals(int[] a, int[] a2)
boolean = equals(long[] a, long[] a2)
boolean = equals(Object[] a, Object[] a2)
boolean = equals(short[] a, short[] a2)
常用方法已整理,一些不常用的方法没有整理进来,当用到的时候,查阅Java API文档。