What can you do with Arrays? Sunflower Collection?

Insert picture description here

Preface

This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory, allowing arrays to be treated as lists. If the specified array reference is null, the methods in this class throw NullPointerException unless otherwise stated. A brief description of the embodiments included in the files included in such methods. These descriptions should be considered implementation instructions, not part of the specification. As long as the specification itself is adhered to, the implementer can replace other algorithms at will.
(For example, the algorithm used by sort(Object[]) is not necessarily sort(Object[]), but it must be stable.)


1. What is Arrays?

The Arrays class is located in the java.util package. It mainly contains various methods for manipulating arrays. In fact, we know that other collections such as List and Set provide many methods, but arrays do not belong to the collection system. , It is identified by [], it does not provide any method for the user to operate, only a length attribute is provided.

Second, use steps

Common method

The source code says:

asList(T... a) 
binarySearch( Object[] a, Object key)
copyOf(Object[] original, int newLength) 
copyOfRange(Object[] original, int from, int to) 
deepEquals(Object[] a1, Object[] a2)
deepHashCode(Object a[]) 
deepToString(Object[] a) 
deepToString(Object[] a, StringBuilder buf, Set<Object[]> dejaVu) {
    
    
equals(Object[] a, Object[] a2)
fill(Object[] a, Object val)
hashCode(char a[])
legacyMergeSort(Object[] a) 
parallelPrefix(Object[] array, Object op)
parallelSetAll(Object[] array, IntToObjectFunction generator) 
parallelSort(Object[] a) 
setAll(Object[] array, IntToObjectFunction generator)
sort(Object[] a)
spliterator(Object[] array) 
spliterator(Object[] array, int startInclusive, int endExclusive)
stream(Object[] array)
stream(Object[] array, int startInclusive, int endExclusive) 
swap(Object[] x, int a, int b)
toString(Object[] a)

Code example:
asList() method: convert an array or a variable parameter to a List

public static void asList(){
    
    
        String [] arr={
    
    "n","a","n"};
        List<String> list = Arrays.asList(arr);
        List<String> list1 = Arrays.asList("b","a","o");
        System.out.println(list);
        System.out.println(list1);
        /**
         * [n, a, n]
         * [b, a, o]
         */
    }

Sort the array in ascending order:

public static void sort(){
    
    
        int [] arr ={
    
    9,5,4,8,3,-4};
        Arrays.sort(arr);
        for (int i : arr){
    
    
            System.out.print(i+" ");
        }
        //-4 3 4 5 8 9
    }

sort​(int[] a, int fromIndex, int toIndex) sorts the specified range of the array in ascending order.

public static void sort1(){
    
    
        int [] arr ={
    
    9,5,4,8,3,-4};
        Arrays.sort(arr,0,3);
        for (int i : arr){
    
    
            System.out.print(i+" ");
            //4 5 9 8 3 -4 
        }
    }

Arrays.sort(nums, new Comparator()) Using Comparator to specify the sorting direction mainly depends on the method specified by the rewritten compared method

public void sort() {
    
    
    Integer[] nums = {
    
    2, 5, 0, 4, 6, -10};
    Arrays.sort(nums, new Comparator<Integer>() {
    
    
        @Override
        public int compare(Integer o1, Integer o2) {
    
    
            return o1.compareTo(o2);
        }
    });
    for (int i : nums) {
    
    
        System.out.print(i + " ");
    }
}
// 输出结果
-10 0 2 4 5 6 

fill​(int[] a, int val) Assign the specified int value to each element of the specified int array, and replace all the elements in the array with val

 public static void fill(){
    
    
        int [] arr ={
    
    9,5,4,8,3,-4};
        Arrays.fill(arr,2);
        for (int i : arr){
    
    
            System.out.print(i+" ");
            //2 2 2 2 2 2 
        }
    }

fill​(int[] a, int fromIndex, int toIndex, int val) assigns the specified int value to each element of the specified range of the specified int array.

public static void fill1(){
    
    
        int [] arr ={
    
    9,5,4,8,3,-4};
        Arrays.fill(arr,2,5,200);
        for (int i : arr){
    
    
            System.out.print(i+" ");
            //9 5 200 200 200 -4 
        }
    }

toString​(int[] a) returns the string representation of the contents of the specified array.

 public static void tostring(){
    
    
        int [] arr ={
    
    9,5,4,8,3,-4};
        //[I@6108b2d7
        System.out.println(arr);
        //[9, 5, 4, 8, 3, -4]
        System.out.println(Arrays.toString(arr));
    }

deepToString(Object[][] arrays) returns the string form of a multi-dimensional array

@Test
public void deepToString() {
    
    
    int[][] nums = {
    
    {
    
    2, 5, 0, 4, 6, -10},{
    
    2, 5, 0, 4, 6, -10}};
    System.out.println(nums);
    System.out.println(Arrays.deepToString(nums));
}
// 输出结果
[[I@587c290d
[[2, 5, 0, 4, 6, -10], [2, 5, 0, 4, 6, -10]]

binarySearch(Object[] a, Object key)
uses binary search to find a specific key. If it finds the subscript, it returns -1. It should be noted that binary search requires the input to be ordered

 public static void binarysearch(){
    
    
        //无序输出
        int a =Arrays.binarySearch(new int[] {
    
    1,2,3,9,5}, 4);

        int b=Arrays.binarySearch(new int[] {
    
    1,2,3,4,5}, 4);
        System.out.println(a);//-4
        
        
        System.out.println(b);//3
    }

copyOf​(int[] original, int newLength) copies the specified array with zeros, truncated or padded (if necessary) so that the copy has the specified length.

public void copyOfRange() {
    
    
    int[] ints = Arrays.copyOfRange(new int[]{
    
    9, 8, 7,6,5,4,3,2,1}, 0, 5);
    System.out.println(Arrays.toString(ints));
}
// 输出结果
[9, 8, 7, 6, 5]

ParallelPrefix uses the function of parallel calculation and calculation of arrays. Since it is a functional interface, it can be used as lambda expressions.

public void parallelPrefix() {
    
    
    int[] ints = {
    
    1, 2, 3, 4, 5};
    //K表示数组第一个值,也就是0号索引,V代表K的下一个索引值,两个索引的值相加
    Arrays.parallelPrefix(ints,(K,V)-> K+V);
    //输出为[1, 3, 6, 10, 15]:流程是1和2相加为3,3和3相加为6,6和4相加为10...以此往后类推
    System.out.println(Arrays.toString(ints));

    int[] Ints = {
    
    5,4,3,2,1,0};
    //从1号索引到5号索引之间开始相加数值
    Arrays.parallelPrefix(Ints,1,5,(K,V) -> K + V);
    System.out.println(Arrays.toString(Ints)); //输出为[5, 4, 7, 9, 10, 0]

}

parallelSort uses parallel sort + merge sort algorithm to sort the array

public void parallelSort() {
    
    
    int[] ints = {
    
    2, 3, 4, 5, 1};
    Arrays.parallelSort(ints);
    //输出为:[1, 2, 3, 4, 5]
    System.out.println(Arrays.toString(ints));
    
    int[] Ints = {
    
    2, 3, 4, 5, 1,6,8,7};
    //从1到7号索引之间进行排序
    Arrays.parallelSort(Ints,1,7);
    //输出为:[2, 1, 3, 4, 5, 6, 8, 7]
    System.out.println(Arrays.toString(Ints));
}

Insert picture description here
Nonsense time:

If the customer thinks that the food is appropriate, can you give a free like! Thank you! Slow walker officer! It is recommended to pack and collect and come again next time. Shop Xiaoer QQ: 309021573, welcome to harass!

Guess you like

Origin blog.csdn.net/AzirBoDa/article/details/112920830