01-java.util.Arrays

1、public static <T> List<T> asList(T... a):返回一个受指定数组支持的固定大小的列表(对返回列表的更改会“直接写”到数组)

注:该方法是将数组转化为list,有以下几点需要注意:

(1)该方法不适用于基本数据类型(byte、short、int、long、float、double、boolean)

(2)该方法将数组和列表链接起来,当更新其中之一时,另一个自动更新

(3)不支持add和remove方法

说明:

(1)通过查看Arrays类的源码可以知道,asList返回的List是Array中实现的内部类,而该类并没有定义add和remove方法

(2)修改其中一个,另一个也自动获得更新,是因为asList获得List实际引用的就是数组

        String[] arr = new String[3];
        arr[0] = "zhangsan";
        List<String> list = Arrays.asList(arr);
        System.out.println(list.toString());    //[zhangsan, null, null]
        arr[1] = "lisi";
        System.out.println(list.toString());    //[zhangsan, lisi, null]
        list.set(0, "wuwu");
        System.out.println(arr[0]); //wuwu
        System.out.println(list.toString());    //[wuwu, lisi, null]
        List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
        System.out.println(stooges);    //[Larry, Moe, Curly]

2、public static int binarySearch(int[] a, int fromIndex, int endIndex,int key):二分法查找指定值在数组中的位置,前提是数组必须有序(不能先排序)。如果找不到,返回 -(插入点) -1

注:fromIndex和endIndex表示查找的起止位置,包含头不包含尾,可不要这两个参数

3、public static int[] copyOf(int[] original, int newLength):将指定数组的指定范围复制到一个新数组,从0开始,newLength结束,newLength超出时补默认值(修改新数组的值不会影响原数组)

        int[] arr = {2, 5, 6, 8, 32, 9};
        int[] arr1 = Arrays.copyOf(arr, 3); //[2,5,6]
        int[] arr2 = Arrays.copyOf(arr, 9); //[2,5,6,8,32,9,0,0,0]
        int[] newArr = new int[3];
        newArr = Arrays.copyOf(arr, 2); //[2,5]
        newArr = Arrays.copyOf(arr, 4); //[2,5,6,8]
        newArr = Arrays.copyOf(arr, 9); //[2,5,6,8,32,9,0,0,0]

4、public static int[] copyOfRange(int[] original, int from, int to):将指定数组的指定范围复制到一个新数组,从from开始,to结束(包含头不包含尾)。from超出时报错,to超出时补默认值

        int[] arr = {2, 5, 6, 8, 32, 9};
        int[] arr1 = Arrays.copyOfRange(arr, 2, 5); //[6,8,32]  
        int[] arr2 = Arrays.copyOfRange(arr, 4, 9); //[32,9,0,0,0]  
        //int[] arr3 = Arrays.copyOfRange(arr, 9, 11); //java.lang.ArrayIndexOutOfBoundsException  
        int[] arr4 = Arrays.copyOfRange(arr, 5, 5); //null  
        int[] arr5 = Arrays.copyOfRange(arr, 5, 6); //[9] 
5、public static boolean deepEquals(Object[] a1, Object[] a2):如果两个指定数组彼此是 深层相等 的,则返回 true(此方法适用于任意深度的嵌套数组)

说明:如果满足以下任意条件之一,则两个 null 元素 e1 和 e2 可能是深层相等的:

(1)e1 和 e2 都是相同基本类型的数组,并且 Arrays.equals(e1, e2) 的适当重载将返回 true

(2)e1 和 e2 都是对象引用类型的数组,并且 Arrays.deepEquals(e1, e2) 将返回 true

(3)e1 == e2

(4)e1.equals(e2) 将返回 true

注意:此定义支持任意深度的 null 元素

        int[] arr1 = {2, 5, 6, 8, 32, 9};
        int[] arr2 = {2, 5, 6, 8, 32, 9};
        Object[] obj1 = {new int[3], arr1};
        Object[] obj2 = {new int[3], arr2};
        Object[] obj3 = {new int[3], new int[]{2, 5, 6, 8, 32, 9}};
        int[][] obj4 = {new int[3], {2, 5, 6, 8, 32, 9}};
        System.out.println(arr1.equals(arr2));  //false
        System.out.println(obj1.equals(obj2));  //false
        System.out.println(Arrays.equals(arr1, arr2));   //true,
        System.out.println(Arrays.equals(obj1, obj2));   //false,引用类型用Arrays.deepEquals
        //System.out.println(Arrays.deepEquals(arr1, arr2));   //报错,int[]无法转换为java.lang.Object[]。基本类型用Arrays.equals
        System.out.println(Arrays.deepEquals(obj1, obj2));   //true
        System.out.println(Arrays.deepEquals(obj1, obj3));   //true
        System.out.println(Arrays.deepEquals(obj1, obj4));   //true
        System.out.println(Arrays.deepEquals(null, null));   //true

6、public static int deepHashCode(Object[] a):基于指定数组的“深层内容”返回哈希码。如果数组包含作为元素的其他数组,则哈希码将基于其内容

说明:

(1)如果数组 a 的 e 元素本身是一个数组,则不能通过调用 e.hashCode() 计算其哈希码

(2)但是,如果 e 是一个基本类型数组,则可以通过调用 Arrays.hashCode(e) 的适当重载来计算其哈希码

(3)或者,如果 e 是一个引用类型数组,则可以通过递归调用 Arrays.deepHashCode(e) 来计算其哈希码

(4)如果 a 为 null,则此方法返回 0

注:对于任何两个满足 Arrays.deepEquals(a, b) 的数组 a 和 b,也可以说 Arrays.deepHashCode(a) == Arrays.deepHashCode(b)

       int[] arr1 = {2, 5, 6, 8, 32, 9};
        int[] arr2 = {2, 5, 6, 8, 32, 9};
        Object[] obj1 = {new int[3], arr1};
        Object[] obj2 = {new int[3], new int[]{2, 5, 6, 8, 32, 9}};
        int[][] obj3 = {new int[3], {2, 5, 6, 8, 32, 9}};
        System.out.println(arr1.hashCode() == arr2.hashCode()); //false
        System.out.println(obj1.hashCode() == obj2.hashCode()); //false
        System.out.println(Arrays.hashCode(arr1) == Arrays.hashCode(arr2)); //true
        System.out.println(Arrays.hashCode(obj1) == Arrays.hashCode(obj2));   //false,引用类型用Arrays.deepHashCode
        //System.out.println(Arrays.deepHashCode(arr1) == Arrays.deepHashCode(arr2)); ////报错,int[]无法转换为java.lang.Object[]。基本类型用Array.hashCode
        System.out.println(Arrays.deepHashCode(obj1) == Arrays.deepHashCode(obj2));   //true
        System.out.println(Arrays.deepHashCode(obj1) == Arrays.deepHashCode(obj3));   //true

7、public static String deepToString(Object[] a):返回指定数组“深层内容”的字符串表示形式。如果数组包含作为元素的其他数组,则字符串表示形式包含其内容等。此方法是为了将多维数组转换为字符串而设计的

        int[] arr1 = {2, 5, 6, 8, 32, 9};
        Object[] obj1 = {new int[3], arr1};
        Object[] obj2 = {new int[3], new int[]{2, 5, 6, 8, 32, 9}};
        int[][] obj3 = {new int[3], {2, 5, 6, 8, 32, 9}};
        System.out.println(arr1.toString());    //[I@cac736f
        System.out.println(obj1.toString());    //[Ljava.lang.Object;@5e265ba4
        System.out.println(Arrays.toString(arr1));  //[2, 5, 6, 8, 32, 9]
        System.out.println(Arrays.toString(obj1));   //[[I@5e265ba4, [I@cac736f],基本类型用Arrays.toString
        //System.out.println(Arrays.deepToString(arr1));   //报错,int[]无法转换为java.lang.Object[]。引用类型用Arrays.deepToString
        System.out.println(Arrays.deepToString(obj1));   //[[0, 0, 0], [2, 5, 6, 8, 32, 9]]
        System.out.println(Arrays.deepToString(obj2));   //[[0, 0, 0], [2, 5, 6, 8, 32, 9]]
        System.out.println(Arrays.deepToString(obj3));   //[[0, 0, 0], [2, 5, 6, 8, 32, 9]]

8、public static boolean equals(int[] a, int[] a2):如果两个指定的 int 型数组彼此相等,则返回 true。如果两个数组引用都为 null,则认为它们是相等的

9、public static int hashCode(int[] a):基于指定数组的内容返回哈希码。如果 a 为 null,则此方法返回 0

10、public static String toString(int[] a):返回指定数组内容的字符串表示形式。如果 a 为 null,则返回 "null"

11、数组中没有覆盖Object的equals、hashCode、toString方法,但Arrays工具类中提供了覆盖后的方法。分两种情况:

(1)数组中元素类型是基本类型:Arrays.equals、Arrays.hashCode、Arrays.toString

(2)数组中元素类型是引用类型:Arrays.deepEquals、Arrays.deepHashCode、Arrays.deepToString

12、public static void fill(int[] a, int fromIndex, int toIndex, int val):将数组中指定范围的内容用指定值替换。fromIndex或toIndex越界,都会报错

注:fromIndex和endIndex表示查找的起止位置,包含头不包含尾,可不要这两个参数

        int[] arr1 = {2, 5, 6, 8, 5, 9};   //length = 6
        Arrays.fill(arr1, 0, 3, 33);    //[33,33,33,8,5,9]
        //Arrays.fill(arr1, 0, 9, 33);    //java.lang.ArrayIndexOutOfBoundsException

13、public static void sort(int[] a, int fromIndex, int toIndex):将数组指定范围的内容进行升序排序 -- 快速排序法。fromIndex或toIndex越界,都会报错

        int[] arr1 = {2, 9, 6, 3, 5, 9};   //length = 6
        Arrays.sort(arr1, 0, 3);    //[2,6,9,3,5,9]
        //Arrays.sort(arr1, 0, 9);    //java.lang.ArrayIndexOutOfBoundsException


猜你喜欢

转载自blog.csdn.net/ruyu00/article/details/79842863