Java self-learning-array Arrays

Keywords: Java

Common methods of java.util.Arrays class

Arrays is a tool class for arrays, which can be sorted, searched, replicated and filled. It greatly improves the work efficiency of developers.

Step 1: Array replication

Similar to using System.arraycopy for array replication, Arrays provides a copyOfRange method for array replication.
The difference is System.arraycopy, which requires that the target array be prepared beforehand and the length allocated. CopOfRange only needs the source array, and by returning the value, the target array can be obtained.
In addition, it should be noted that the third parameter of copyOfRange, which represents the end of the source array, is not available.

import java.util.Arrays;
 
public class HelloWorld {
    public static void main(String[] args) {
        int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
 
        // copyOfRange(int[] original, int from, int to)
        // The first parameter represents the source array
        // The second parameter represents the starting position (obtained)
        // The third parameter represents the end position (not available)
        int[] b = Arrays.copyOfRange(a, 0, 3);
 
        for (int i = 0; i < b.length; i++) {
            System.out.print(b[i] + " ");
        }
 
    }
}

Step 2: Convert to a string

If you want to print the contents of an array, you need to iterate through the for loop, printing one by one.

But Arrays provides a toString() method that converts an array directly to a string, making it easy to observe the contents of the array.

import java.util.Arrays;
  
public class HelloWorld {
    public static void main(String[] args) {
        int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
        String content = Arrays.toString(a);
        System.out.println(content);
  
    }
}

Step 3: Sorting

In the previous study of selection and bubbling sorting, the Arrays toolkit class provides a sort method, which can complete the sorting function in only one line of code.

import java.util.Arrays;
  
public class HelloWorld {
    public static void main(String[] args) {
        int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
        System.out.println("Before sorting :");
        System.out.println(Arrays.toString(a));
        Arrays.sort(a);
        System.out.println("After sorting:");
        System.out.println(Arrays.toString(a));
  
    }
}

Step 4: Search

Location of query elements
Note that before using binary Search for lookups, sort must be used
If there are multiple identical elements in an array, the result of the lookup is uncertain.

 import java.util.Arrays;
     
    public class HelloWorld {
        public static void main(String[] args) {
            int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
     
            Arrays.sort(a);
     
            System.out.println(Arrays.toString(a));
            //sort must be used before binary Search can be used
            System.out.println("Location of the number 62:"+Arrays.binarySearch(a, 62));
        }
    }

Step 5: Determine whether it is the same

Compare the contents of two arrays
The last element of the second array is 8, which is different from the first array, so the comparison result is false.

import java.util.Arrays;
 
public class HelloWorld {
    public static void main(String[] args) {
        int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
        int b[] = new int[] { 18, 62, 68, 82, 65, 8 };
 
        System.out.println(Arrays.equals(a, b));
    }
}

Step 6: Fill in

Fill the entire array with the same value

import java.util.Arrays;
  
public class HelloWorld {
    public static void main(String[] args) {
        int a[] = new int[10];
  
        Arrays.fill(a, 5);
  
        System.out.println(Arrays.toString(a));
  
    }
}

Practice: Two-Dimensional Array Sorting

(Firstly, a two-dimensional array of 5X8 is defined, and then filled with random numbers.
Array's method is used to sort two-dimensional Arrays.
Reference train of thought:
First, the two-dimensional array is copied to a one-dimensional array using System.arraycopy.
Then sort with sort
Finally, copy back to the two-dimensional array. )

Posted by Syto on Mon, 26 Aug 2019 00:44:22 -0700