提供了数组操作的相关方法,连接数组和集合
Integer[] arrs = new Integer[] {1,2,3,4,5,6,7,8,9};
List<Integer> list = Arrays.asList(arrs);
System.out.println(list);
arrs[5] = 100;
System.out.println(list);
//[1, 2, 3, 4, 5, 6, 7, 8, 9]
//[1, 2, 3, 4, 5, 100, 7, 8, 9]
}
Integer[] arrs = new Integer[] {1,2,3,4,5,6,7,8,9};
int num = Arrays.binarySearch(arrs, 5);
System.out.println(num);
//4
Integer[] arrs = new Integer[] {1,2,3,4,5,6,7,8,9};
Integer[] arrs1 = Arrays.copyOf(arrs, 5);
for(Integer i : arrs1) {
System.out.print(i + " ");
}
System.out.println();
//1 2 3 4 5
arrs1[3] = 100;
for(Integer i : arrs) {
System.out.print(i + " ");
}
//1 2 3 4 5 6 7 8 9
System.out.println();
for(Integer i : arrs1) {
System.out.print(i + " ");
}
//1 2 3 100 5
Integer[] ret = Arrays.copyOf(arrs,20,Integer[].class);
System.out.println();
for(Integer i : ret) {
System.out.print(i + " ");
}
//1 2 3 4 5 6 7 8 9 null null null null null null null null null null null
Integer[] range = Arrays.copyOfRange(arrs, 5, 20);
System.out.println();
for(Integer i : ret) {
System.out.print(i + " ");
}
//1 2 3 4 5 6 7 8 9 null null null null null null null null null null null
Integer[] arrs = new Integer[] {1,2,3,4,5,6,7,8,9};
Integer[] arrs1 = new Integer[] {1,2,3,4,5,6,7,8,9};
System.out.println(arrs);
System.out.println(arrs1);
System.out.println(Arrays.equals(arrs, arrs1));
// [Ljava.lang.Integer;@2077d4de
// [Ljava.lang.Integer;@7591083d
// true
public void test5() {
Integer[] arrs = new Integer[10];
Arrays.fill(arrs, 100);
for(Integer i : arrs) {
System.out.print(i + " ");
}
//100 100 100 100 100 100 100 100 100 100
}
public void test6() {
Integer[] arrs = new Integer[20];
for(int i=0;i<20;i++)
arrs[i] = (int)(Math.random()*100);
for(int i:arrs) {
System.out.print(i + " ");
}
System.out.println();
//49 40 0 79 4 98 12 81 71 27 9 45 16 17 7 92 63 22 73 22
Arrays.sort(arrs);
for(int i:arrs)
System.out.print(i + " ");
System.out.println();
//0 4 7 9 12 16 17 22 22 27 40 45 49 63 71 73 79 81 92 98
Arrays.sort(arrs,(o1,o2)->o2-o1);
for(int i:arrs)
System.out.print(i + " ");
//98 92 81 79 73 71 63 49 45 40 27 22 22 17 16 12 9 7 4 0
}
public void test7() {
//生产1万个元素
int len = 100000;
Integer[] arrs = new Integer[len];
for(int i=0;i<len;i++)
arrs[i] = (int)(Math.random()*100);
long t1 = System.currentTimeMillis();
Arrays.sort(arrs);
long t2 = System.currentTimeMillis();
System.out.println("sort\t"+(t2-t1));
for(int i=0;i<len;i++)
arrs[i] = (int)(Math.random()*100);
long t3 = System.currentTimeMillis();
Arrays.parallelSort(arrs);
long t4 = System.currentTimeMillis();
System.out.println("parallelSort\t"+(t4-t3));
System.out.println("------------------------------");
}
public void test8() {
for(int i=0;i<100;i++) {
test7();
}
}
sort 35
parallelSort 64
------------------------------
sort 40
parallelSort 73
------------------------------
sort 107
parallelSort 53
------------------------------
sort 73
parallelSort 63
------------------------------
sort 74
parallelSort 62
····
····
int len = 100;
Integer[] arrs = new Integer[len];
for(int i=0;i<len;i++)
arrs[i] = (int)(Math.random()*100);
Spliterator<Integer> s = Arrays.spliterator(arrs);
s.forEachRemaining((e)->System.out.print(e + " "));
public void test10() {
int len = 10;
Integer[] arrs = new Integer[len];
for(int i=0;i<len;i++)
arrs[i] = (int)(Math.random()*100);
List<Integer> list = Arrays.stream(arrs).collect(Collectors.toList());
System.out.println(list);
//[56, 33, 59, 63, 55, 37, 68, 52, 53, 3]
}
public void test11() {
int len = 10;
Integer[] array = new Integer[len];
Arrays.setAll(array, (e)->10);
for(int i:array)
System.out.print(i + " ");
System.out.println();
Arrays.parallelSetAll(array, (e)->e + 10);
for(int i:array)
System.out.print(i + " ");
// 10 10 10 10 10 10 10 10 10 10
// 10 11 12 13 14 15 16 17 18 19
}
public void test12() {
int len = 10;
int[] array = new int[len];
Arrays.parallelPrefix(array, (e,u)->{
System.out.println(e + "\t" + u);
return e +1;
});
System.out.println();
for(int i:array)
System.out.print(i + " ");
}
public void test13() {
int len = 10;
Integer[] arrs = new Integer[len];
for(int i=0;i<len;i++)
arrs[i] = (int)(Math.random()*100);
System.out.println(Arrays.toString(arrs));
System.out.println(Arrays.hashCode(arrs));
}