Arrays.toString
@Test
public void test_15() {
int[] oldArr = {1, 2, 3, 4, 7, 2, 3};
System.out.println(Arrays.toString(oldArr)); // [1, 2, 3, 4, 7, 2, 3]
}
Arrays.sort
@Test
public void test_16() {
int[] oldArr = {1, 2, 3, 4, 7, 2, 3};
Arrays.sort(oldArr);
System.out.println(Arrays.toString(oldArr)); // [1, 2, 2, 3, 3, 4, 7]
}
Arrays.binarySearch
@Test
public void test_17() {
int[] oldArr = {1, 2, 3, 4, 7, 2, 3};
//二分查找的前提是 升序排序
Arrays.sort(oldArr);
System.out.println(Arrays.toString(oldArr)); // [1, 2, 2, 3, 3, 4, 7]
// 在数组oldArr中查找7,返回7的索引(排序后的索引)
int index = Arrays.binarySearch(oldArr, 7);
System.out.println(index); //6
//返回结果小于0,表示在数组中不存在。返回的负数是排序以后数组 插入点的索引的负数-1
// [1, 2, 2, 3, 3, 4, 5(插入点), 7] 插入点的索引是 6, 6的负数-6, -6-1 = -7
System.out.println(Arrays.binarySearch(oldArr, 5)); //-7
}
Arrays.equals
@Test
public void test_18() {
int[] arr1 = {1, 2, 3, 4, 7, 2, 3};
int[] arr2 = {1, 2, 3, 4, 7, 2, 3};
System.out.println(arr1.equals(arr2)); // false 比较的是地址,继承自Object
System.out.println(Arrays.equals(arr1, arr2)); //true
}
Arrays.fill
@Test
public void test_19() {
int[] arr1 = new int[10];
Arrays.fill(arr1, 2); //用2 填充数组,就算有值也会被覆盖
System.out.println(Arrays.toString(arr1)); //[2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
}
Arrays.copyOf
@Test
public void test_20() {
int[] arr1 = {1, 2, 3, 4, 7, 2, 3};
//从0开始复制3个元素,返回新的数组
int[] arr2 = Arrays.copyOf(arr1,3);
System.out.println(Arrays.toString(arr2)); //[1, 2, 3]
//从指定开始索引到指定的结束索引的元素,返回新的数组
int[] arr3 = Arrays.copyOfRange(arr1,1,5);
System.out.println(Arrays.toString(arr3)); //[2, 3, 4, 7]
}