Arrays.toString

  1. @Test
  2. public void test_15() {
  3. int[] oldArr = {1, 2, 3, 4, 7, 2, 3};
  4. System.out.println(Arrays.toString(oldArr)); // [1, 2, 3, 4, 7, 2, 3]
  5. }

Arrays.sort

  1. @Test
  2. public void test_16() {
  3. int[] oldArr = {1, 2, 3, 4, 7, 2, 3};
  4. Arrays.sort(oldArr);
  5. System.out.println(Arrays.toString(oldArr)); // [1, 2, 2, 3, 3, 4, 7]
  6. }

Arrays.binarySearch

  1. @Test
  2. public void test_17() {
  3. int[] oldArr = {1, 2, 3, 4, 7, 2, 3};
  4. //二分查找的前提是 升序排序
  5. Arrays.sort(oldArr);
  6. System.out.println(Arrays.toString(oldArr)); // [1, 2, 2, 3, 3, 4, 7]
  7. // 在数组oldArr中查找7,返回7的索引(排序后的索引)
  8. int index = Arrays.binarySearch(oldArr, 7);
  9. System.out.println(index); //6
  10. //返回结果小于0,表示在数组中不存在。返回的负数是排序以后数组 插入点的索引的负数-1
  11. // [1, 2, 2, 3, 3, 4, 5(插入点), 7] 插入点的索引是 6, 6的负数-6, -6-1 = -7
  12. System.out.println(Arrays.binarySearch(oldArr, 5)); //-7
  13. }

Arrays.equals

  1. @Test
  2. public void test_18() {
  3. int[] arr1 = {1, 2, 3, 4, 7, 2, 3};
  4. int[] arr2 = {1, 2, 3, 4, 7, 2, 3};
  5. System.out.println(arr1.equals(arr2)); // false 比较的是地址,继承自Object
  6. System.out.println(Arrays.equals(arr1, arr2)); //true
  7. }

Arrays.fill

  1. @Test
  2. public void test_19() {
  3. int[] arr1 = new int[10];
  4. Arrays.fill(arr1, 2); //用2 填充数组,就算有值也会被覆盖
  5. System.out.println(Arrays.toString(arr1)); //[2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
  6. }

Arrays.copyOf

  1. @Test
  2. public void test_20() {
  3. int[] arr1 = {1, 2, 3, 4, 7, 2, 3};
  4. //从0开始复制3个元素,返回新的数组
  5. int[] arr2 = Arrays.copyOf(arr1,3);
  6. System.out.println(Arrays.toString(arr2)); //[1, 2, 3]
  7. //从指定开始索引到指定的结束索引的元素,返回新的数组
  8. int[] arr3 = Arrays.copyOfRange(arr1,1,5);
  9. System.out.println(Arrays.toString(arr3)); //[2, 3, 4, 7]
  10. }