Arrays的使用方法总结、实现原理、使用示例

目录

 

常用方法总结

public static List asList(T... a):将多个参数转换为ArrayList

public static int binarySearch(byte[] a, byte key):查询元素第一次出现的位置

char[]、double[]、float[]、int[]、long[]、Object[]、short[]、T[]均有此方法

public static int binarySearch(byte[] a, int fromIndex, int toIndex,byte key):指定范围key第一次出现的位置

char[]、double[]、float[]、int[]、long[]、Object[]、short[]、T[]均有此方法

public static char[] copyOf(char[] original, int newLength):复制数组

boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]、T[]均有此方法

public static boolean[] copyOfRange(boolean[] original, int from, int to) :复制数组某一段

boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]、T[]均有此方法

public static boolean equals(byte[] a, byte[] a2):长度相等,元素相等

boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法

public static void fill(boolean[] a, boolean val):用val替换数组中所有的值

boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法

public static void fill(int[] a, int fromIndex, int toIndex, int val):用val替换数组指定范围的值

boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法

public static int hashCode(int a[]):计算hashCode方法

boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法

public static void parallelSort(byte[] a):对数组进行并发排序

boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法

public static void parallelPrefix(int[] array, IntBinaryOperator op):修改的值是1---length-1,位置0的元素不会被修改

 double[]、int[]、long[]、T[]均有此方法

public static void parallelPrefix(int[] array, int fromIndex,int toIndex, IntBinaryOperator op):修改指定范围内的值

 double[]、int[]、long[]、T[]均有此方法

public static void parallelSetAll(int[] array, IntUnaryOperator generator):并发修改

 double[]、int[]、long[]、T[]均有此方法

public static void setAll(int[] array, IntUnaryOperator generator):单线程修改修改

 double[]、int[]、long[]、T[]均有此方法

public static void sort(int[] a):升序排序

char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法

public static IntStream stream(int[] array)

public static String toString(boolean[] a):格式化输出

char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法


常用方法总结

public static <T> List<T> asList(T... a):将多个参数转换为ArrayList

public class Test {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        System.out.println(list);

        List<String> list1 = Arrays.asList("1","2","3");
        System.out.println(list1);

        List<Long> list2 = Arrays.asList(1L,2L);
        System.out.println(list2);
    }
}

结果:

[1, 2, 3, 4, 5]
[1, 2, 3]
[1, 2]

public static int binarySearch(byte[] a, byte key):查询元素第一次出现的位置

char[]、double[]、float[]、int[]、long[]、Object[]、short[]、T[]均有此方法

public class Test {
    public static void main(String[] args) {
        byte[] a = {'a','b','c','a'};
        //底层采用二分法,所以要先对数组进行排序
        Arrays.sort(a);
        System.out.println(Arrays.binarySearch(a, (byte) 'a'));
    }
}

结果:

0

public static int binarySearch(byte[] a, int fromIndex, int toIndex,byte key):指定范围key第一次出现的位置

char[]、double[]、float[]、int[]、long[]、Object[]、short[]、T[]均有此方法

public class Test {
    public static void main(String[] args) {
        byte[] a = {(byte)'a',(byte)'b',(byte)'c',(byte)'a'};
        //底层采用二分法,所以要先对数组进行排序
        Arrays.sort(a);
        System.out.println(Arrays.binarySearch(a,1,a.length, (byte) 'a'));
    }
}

结果:

1

public static char[] copyOf(char[] original, int newLength):复制数组

boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]、T[]均有此方法

public class Test {
    public static void main(String[] args) {
        byte[] a = {(byte)'a',(byte)'b',(byte)'c',(byte)'a'};
        //底层采用二分法,所以要先对数组进行排序
        byte[] bytes = Arrays.copyOf(a, a.length);
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
        }
    }
}

结果:

97
98
99
97

public static boolean[] copyOfRange(boolean[] original, int from, int to) :复制数组某一段

boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]、T[]均有此方法

public class Test {
    public static void main(String[] args) {
        byte[] a = {(byte)'a',(byte)'b',(byte)'c',(byte)'a'};
        //底层采用二分法,所以要先对数组进行排序
        byte[] bytes = Arrays.copyOfRange(a,2, a.length);
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
        }
    }
}

结果:

99
97

public static boolean equals(byte[] a, byte[] a2):长度相等,元素相等

boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法

public class Test {
    public static void main(String[] args) {
        byte[] a = {(byte)'a',(byte)'b',(byte)'c',(byte)'a'};
        byte[] bytes = Arrays.copyOf(a, a.length);
        //长度相等,每个元素相等,返回true
        System.out.println(Arrays.equals(a,bytes));
    }
}

结果:

true

public static void fill(boolean[] a, boolean val):用val替换数组中所有的值

boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法

public class Test {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4};

        Arrays.fill(a, 5);
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }
}

结果:

5
5
5
5

public static void fill(int[] a, int fromIndex, int toIndex, int val):用val替换数组指定范围的值

boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法

public class Test {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4};

        Arrays.fill(a, 0,2,5);
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }
}

结果

5
5
3
4

public static int hashCode(int a[]):计算hashCode方法

boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法

public class Test {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4};

        System.out.println(Arrays.hashCode(a));
    }
}

结果:

955331

public static void parallelSort(byte[] a):对数组进行并发排序

boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法

public class Test {
    public static void main(String[] args) {
        int[] a = {1,8,51,13,46,11,22};
        Arrays.parallelSort(a);
        System.out.println(Arrays.toString(a));
    }
}

结果:

[1, 8, 11, 13, 22, 46, 51]

public static void parallelPrefix(int[] array, IntBinaryOperator op):修改的值是1---length-1,位置0的元素不会被修改

 double[]、int[]、long[]、T[]均有此方法

public class Test {
    public static void main(String[] args) {
        int[] a = {2,8,51,13,46,11,22};
        System.out.println(Arrays.toString(a));
        Arrays.parallelPrefix(a, new IntBinaryOperator() {
            @Override
            public int applyAsInt(int left, int right) {
                //left当前元素前一个索引的值(2,8,51,13,46,11)即:0---length-2
                //right:当前索引的元素(8,51,13,46,11,22)即:1---length-1
                System.out.println(left+":"+right);
                //修改的值是1---length-1,位置0的元素不会被修改
                return 2*right;
            }
        });
        System.out.println(Arrays.toString(a));
    }
}

结果:除了元素1,其他元素都被扩大了两倍

[2, 8, 51, 13, 46, 11, 22]
2:8
16:51
102:13
26:46
92:11
22:22
[2, 16, 102, 26, 92, 22, 44]

public static void parallelPrefix(int[] array, int fromIndex,int toIndex, IntBinaryOperator op):修改指定范围内的值

 double[]、int[]、long[]、T[]均有此方法

public class Test {
    public static void main(String[] args) {
        int[] a = {2,8,51,13,46,11,22};
        System.out.println(Arrays.toString(a));
        Arrays.parallelPrefix(a,1,4, new IntBinaryOperator() {
            @Override
            public int applyAsInt(int left, int right) {
                //left当前元素前一个索引的值(2,8,51,13,46,11)即:0---length-2
                //right:当前索引的元素(8,51,13,46,11,22)即:1---length-1
                System.out.println(left+":"+right);
                //修改的值是1---length-1,位置0的元素不会被修改
                return 2*right;
            }
        });
        System.out.println(Arrays.toString(a));
    }
}

结果:(1,4)内元素被修改

[2, 8, 51, 13, 46, 11, 22]
8:51
102:13
[2, 8, 102, 26, 46, 11, 22]

public static void parallelSetAll(int[] array, IntUnaryOperator generator):并发修改

 double[]、int[]、long[]、T[]均有此方法

public class Test {
    public static void main(String[] args) {
        int[] a = {2,8,51,13,46,11,22};
        System.out.println(Arrays.toString(a));
        Arrays.parallelSetAll(a, new IntUnaryOperator() {

            @Override
            public int applyAsInt(int operand) {
                //operand当前数组的索引
                System.out.println(operand);
                return operand*2;
            }
        });
        System.out.println(Arrays.toString(a));
    }
}

 结果:因为是并发执行,所以索引输出的顺序是不固定的

[2, 8, 51, 13, 46, 11, 22]
4
3
5
0
6
1
2
[0, 2, 4, 6, 8, 10, 12]

public static void setAll(int[] array, IntUnaryOperator generator):单线程修改修改

 double[]、int[]、long[]、T[]均有此方法

public class Test {
    public static void main(String[] args) {
        int[] a = {2,8,51,13,46,11,22};
        System.out.println(Arrays.toString(a));
        Arrays.setAll(a, new IntUnaryOperator() {

            @Override
            public int applyAsInt(int operand) {
                //operand当前数组的索引
                System.out.println(operand);
                return operand*2;
            }
        });
        System.out.println(Arrays.toString(a));
    }
}

结果:由于是单线程,所以operand是顺序输出的

[2, 8, 51, 13, 46, 11, 22]
0
1
2
3
4
5
6
[0, 2, 4, 6, 8, 10, 12]

public static void sort(int[] a):升序排序

char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法

public class Test {
    public static void main(String[] args) {
        int[] a = {2,8,51,13,46,11,22};
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));
    }
}

结果:

[2, 8, 11, 13, 22, 46, 51]

public static IntStream stream(int[] array)

public class Test {
    public static void main(String[] args) {
        int[] a = {2,8,51,13,46,11,22};
        IntStream stream = Arrays.stream(a);

        System.out.println( Arrays.toString(stream.toArray()));
        System.out.println(Arrays.toString(a));
    }
}

结果:

[2, 8, 51, 13, 46, 11, 22]
[2, 8, 51, 13, 46, 11, 22]

public static String toString(boolean[] a):格式化输出

char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法

public class Test {
    public static void main(String[] args) {
        int[] a = {2,8,51,13,46,11,22};
        System.out.println(Arrays.toString(a));
    }
}

结果:

[2, 8, 51, 13, 46, 11, 22]

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章