public static <T> List<T> asList(T... a);
public static void main(String[] args) {
List<Integer> list1 = Arrays.asList(1, 2, 3);
list1.forEach(System.out::println); // 1 2 3
Integer[] data = {1, 2, 3};
List<Integer> list2 = Arrays.asList(data);
list2.forEach(System.out::println); // 1 2 3
}
注意:该方法返回的是 Arrays 内部静态类 ArrayList,而不是我们平常使用 java.util.List
包下的 ArrayList。由于该静态类的底层采用大小固定的数组,所以任何对该 list 的添加、删除操作都会抛出异常。
//有多个不同数据类型的重载方法,此处列出一个
public static void fill(Object[] a, Object val);
public static void main(String[] args) {
Integer[] data = {1, 2, 3, 4};
Arrays.fill(data, 5);
System.out.println(Arrays.toString(data)); // [5, 5, 5, 5]
}
//有多个不同数据类型的重载方法,此处列出一个
public static void fill(Object[] a, int fromIndex, int toIndex, Object val);
public static void main(String[] args) {
Integer[] data = {1, 2, 3, 4};
Arrays.fill(data, 0, 2, 5);
System.out.println(Arrays.toString(data)); // [5, 5, 3, 4]
}
//有多个不同数据类型的重载方法,此处列出一个
public static void sort(Object[] a);
public static void main(String[] args) {
String[] data = {"1", "4", "3", "2"};
System.out.println(Arrays.toString(data)); // [1, 4, 3, 2]
Arrays.sort(data);
System.out.println(Arrays.toString(data)); // [1, 2, 3, 4]
}
//有多个不同数据类型的重载方法,此处列出一个
public static void sort(Object[] a, int fromIndex, int toIndex);
public static void main(String[] args) {
String[] data = {"1", "4", "3", "2"};
System.out.println(Arrays.toString(data)); // [1, 4, 3, 2]
Arrays.sort(data, 0, 3);
System.out.println(Arrays.toString(data)); // [1, 3, 4, 2]
}
public static <T> void sort(T[] a, Comparator<? super T> c);
public static void main(String[] args) {
String[] data = {"1", "4", "3", "2"};
System.out.println(Arrays.toString(data)); // [1, 4, 3, 2]
// 实现字典序降序排列
// 比较器返回大于1,o1大于o2,换位置;返回0,相等;返回负数,o1小于o2,位置不变
Arrays.sort(data, (o1, o2) -> o2.compareTo(o1));
System.out.println(Arrays.toString(data)); // [4, 3, 2, 1]
}
public static <T> void sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)
public static void main(String[] args) {
String[] data = {"1", "4", "3", "2"};
System.out.println(Arrays.toString(data)); // [1, 4, 3, 2]
// 实现字典序降序排列
// 比较器返回大于1,o1和o2换位置;返回0,相等;返回负数,不变
Arrays.sort(data, 0, 3, (o1, o2) -> o2.compareTo(o1));
System.out.println(Arrays.toString(data)); // [4, 3, 1, 2]
}
public static <T extends Comparable<? super T>> void parallelSort(T[] a);
public static void main(String[] args) {
String[] data = {"1", "4", "3", "2"};
System.out.println(Arrays.toString(data)); // [1, 4, 3, 2]
Arrays.parallelSort(data);
System.out.println(Arrays.toString(data)); // [1, 2, 3, 4]
}
-(最后一次查找的索引值 + 1)
,因此返回负数即为查找未成功。//有多个不同数据类型的重载方法,此处列出一个
public static int binarySearch(Object[] a, Object key);
public static void main(String[] args) {
Integer[] data = {1, 5, 3, 7};
//查找前的数组必须是正序的
Arrays.sort(data);
System.out.println(Arrays.toString(data)); //[1, 3, 5, 7]
System.out.println(Arrays.binarySearch(data, 3)); //1
//查找小于数组最小元素的数
//程序会把数组看作 {0, 1, 3, 5, 7},此时0的索引值为0,则搜索0时返回 -(0 + 1) = -1
System.out.println(Arrays.binarySearch(data, 0)); //-1
//查找大于数组最大元素的数
//程序会把数组看作 {1, 3, 5, 7, 10},此时10的索引值为4,则搜索10时返回 -(5 + 1) = -5
System.out.println(Arrays.binarySearch(data, 10)); //-5
//查找不是数组元素,但在数组范围内的数
//程序会把数组看作 {1, 3, 5, 6, 7},此时6的索引值为3,则搜索10时返回 -(3 + 1) = -4
System.out.println(Arrays.binarySearch(data, 6)); //-4
}
//有多个不同数据类型的重载方法,此处列出一个
public static int binarySearch(Object[] a, int fromIndex, int toIndex, Object key);
public static void main(String[] args) {
Integer[] data = {1, 5, 3, 7};
//查找前的数组必须是正序的
Arrays.sort(data);
System.out.println(Arrays.toString(data)); //[1, 3, 5, 7]
System.out.println(Arrays.binarySearch(data, 0, 2, 3)); //1
//程序会把数组看作 {0, 1, 3},此时0的索引值为0,则搜索0时返回 -(0 + 1) = -1
System.out.println(Arrays.binarySearch(data, 0, 2, 0)); //-1
//程序会把数组看作 {1, 3, 10},此时10的索引值为2,则搜索10时返回 -(2 + 1) = -3
System.out.println(Arrays.binarySearch(data, 0, 2, 10)); //-3
}
System.arraycopy()
方法,从下标 0 开始,如果超过原数组长度,则会用 null 进行填充。//有多个不同数据类型的重载方法,此处列出一个
static <T> T[] copyOf(T[] original, int newLength);
public static void main(String[] args) {
Integer[] data1 = {5, 4, 3, 2};
Integer[] data2 = Arrays.copyOf(data1, 2);
System.out.println(Arrays.toString(data2)); // [5, 4]
Integer[] data3 = Arrays.copyOf(data1, 5);
System.out.println(Arrays.toString(data3)); // [5, 4, 3, 2, null]
}
//有多个不同数据类型的重载方法,此处列出一个
public static <T> T[] copyOfRange(T[] original, int from, int to);
public static void main(String[] args) {
Integer[] data1 = {5, 4, 3, 2};
Integer[] data2 = Arrays.copyOfRange(data1, 0, 2);
System.out.println(Arrays.toString(data2)); // [5, 4]
Integer[] data3 = Arrays.copyOfRange(data1, 0, 5);
System.out.println(Arrays.toString(data3)); // [5, 4, 3, 2, null]
}
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
public static void main(String[] args) {
Integer[] data1 = {5, 4, 3, 2, 1};
Integer[] data2 = new Integer[5];
System.arraycopy(data1, 1, data2, 0, 3);
System.out.println(Arrays.toString(data1)); //[5, 4, 3, 2, 1]
System.out.println(Arrays.toString(data2)); //[4, 3, 2, null, null]
}
//有多个不同数据类型的重载方法,此处列出一个
public static boolean equals(Object[] a, Object[] a2);
public static void main(String[] args) {
Integer[] data1 = {1, 2, 3};
Integer[] data2 = {1, 2, 3};
System.out.println(Arrays.equals(data1, data2)); // true
//数组对象没有重写equals方法,默认判断的是data1 == data2,是否为同个数组对象引用
System.out.println(data1.equals(data2)); //false
}
public static boolean deepEquals(Object[] a1, Object[] a2);
public static void main(String[] args) {
Integer[][] data1 = {{1,2,3}, {1,2,3}};
Integer[][] data2 = {{1,2,3}, {1,2,3}};
System.out.println(Arrays.deepEquals(data1, data2)); // true
}
//有多个不同数据类型的重载方法,此处列出一个
public static int hashCode(Object a[]);
public static void main(String[] args) {
Integer[] data1 = {1, 2, 3};
Integer[] data2 = {1, 2, 3};
System.out.println(Arrays.hashCode(data1)); //30817
//数组对象没有重写hashCode方法,默认返回的是地址的哈希值
System.out.println(data1.hashCode()); //460141958
System.out.println(Arrays.hashCode(data2)); //30817
System.out.println(data2.hashCode()); //1163157884
}
public static int deepHashCode(Object a[]);
public static void main(String[] args) {
Integer[][] data1 = {{1, 2, 3}, {1, 2, 3}};
Integer[][] data2 = {{1, 2, 3}, {1, 2, 3}};
System.out.println(data1.hashCode()); //460141958
System.out.println(Arrays.deepHashCode(data1)); //987105
System.out.println(data2.hashCode()); //1163157884
System.out.println(Arrays.deepHashCode(data2)); //987105
}
toString()
方法,因为该方法拼接的是元素的 toString()
返回值。//有多个不同数据类型的重载方法,此处列出一个
public static String toString(Object[] a);
public static void main(String[] args) {
Integer[] data = {1, 2, 3};
System.out.println(Arrays.toString(data)); // [1, 2, 3]
//数组对象没有重写toString方法,默认返回的是地址的哈希值
System.out.println(data.toString()); //[Ljava.lang.Integer;@1b6d3586
}
public static String deepToString(Object[] a);
public static void main(String[] args) {
Integer[][] data = {{1, 2, 3}, {1, 2, 3}};
System.out.println(Arrays.deepToString(data)); // [[1, 2, 3], [1, 2, 3]]
}
//有多个不同数据类型的重载方法,此处列出一个
public static <T> void setAll(T[] array, IntFunction<? extends T> generator);
public static void main(String[] args) {
Integer[] data = {1, 2, 3, 4};
// i为数组索引值
Arrays.setAll(data, i -> data[i] * 2);
System.out.println(Arrays.toString(data)); // [2, 4, 6, 8]
}
//有多个不同数据类型的重载方法,此处列出一个
public static <T> void parallelSetAll(T[] array, IntFunction<? extends T> generator);
public static void main(String[] args) {
Integer[] data = {1, 2, 3, 4};
// i为索引值
Arrays.parallelSetAll(data, i -> data[i] * 2);
System.out.println(Arrays.toString(data)); // [2, 4, 6, 8]
}
//有多个不同数据类型的重载方法,此处列出一个
public static <T> Stream<T> stream(T[] array);
public static void main(String[] args) {
Integer[] data = {1, 2, 3, 4};
List<Integer> list = Arrays.stream(data).collect(toList());
System.out.println(list); //[1, 2, 3, 4]
}
//有多个不同数据类型的重载方法,此处列出一个
public static <T> Stream<T> stream(T[] array, int startInclusive, int endExclusive);
public static void main(String[] args) {
Integer[] data = {1, 2, 3, 4};
List<Integer> list = Arrays.stream(data, 0, 3).collect(toList());
System.out.println(list); // [1, 2, 3]
}
参考:https://blog.csdn.net/Goodbye_Youth/article/details/81003817
原文:https://www.cnblogs.com/zongmin/p/11339000.html