In the previous chapters, you learned about the basic concepts of arrays and the learning of related codes for sorting, copying, and merging. In this chapter, the concept of Arrays is introduced to copy, arrange, search, and transform characters. The learning of related codes such as string can greatly improve work efficiency and make the code more concise.
1. Array Copy
Similar to using System.arraycopy for array copying, Arrays provides a copyOfRange method for array copying.
The difference is System.arraycopy, which needs to prepare the target array in advance and allocate the length. The copyOfRange only needs the source array, and by returning the value, you can get the target array.
In addition, it should be noted that the third parameter of the copyOfRange indicates the end position of the source array and cannot be obtained.
The specific code is as follows:
import java.util.Arrays;
public class essay1 {
public static void main(String[] args) {
int a[] = new int[] { 11, 22, 78, 89, 15, 90 };
// copyOfRange(int[] original, int from, int to)
// The first parameter represents the source array
// The second parameter indicates the starting position (obtained)
// The third parameter indicates 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] + ” “);
}
}
}

2. Convert to String
If you want to print the contents of an array, you need to find one by one through a for loop, and print one by one.
But Arrays provides a to String () method, which directly converts an array into a string, so that it is convenient to observe the contents of the array.
The specific code is as follows:
import java.util.Arrays;
public class essay1 {
public static void main(String[] args) {
int a[] = new int[] { 11, 22, 78, 89, 15, 90 };
String content = Arrays.toString(a);
System.out.println(content);
}
}

3. Sort
In the previous chapters, I have explained selection method sorting and bubble method sorting. Although the effect of array sorting can be achieved in the end, the process is often very complicated. In this chapter, the concept of Arrays is introduced, and a sort method is provided in the Arrays tool class, which only requires one line of code to complete the sort function.
The specific code is as follows:
import java.util.Arrays;
public class essay1{
public static void main(String[] args) {
int a[] = new int[10];
for(int i=0;i<10;i++)
{a[i]=(int)(Math.random()*10);
}
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));
}
}

4. Search
Query where the element appears.
It should be noted that before using binary Search to find, you must use sort to sort.
If there are multiple identical elements in the array, the search result is uncertain.
The specific code is as follows:
import java.util.Arrays;
public class essay1{
public static void main(String[] args) {
int a[] = new int[] { 13, 52, 88, 82, 66, 9 };
Arrays.sort(a);
System.out.println(Arrays.toString(a));
//Before using binarySearch, you must first use sort to sort
System.out.println(“Where the number 52 appears:”+Arrays.binarySearch(a, 52));
}
}

5. Judge Whether the Same
Compare whether the contents of two arrays are the same
The last element of the second array is 5, which is different from the first array, so the comparison result is false.
The specific code is as follows:
import java.util.Arrays;
public class essay1{
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, 5 };
System.out.println(Arrays.equals(a, b));
}
}

6. Filling
Use the same value to fill the entire array
The specific code is as follows:
import java.util.Arrays;
public class essay1{
public static void main(String[] args) {
int a[] = new int[10];
Arrays.fill(a, 5);
System.out.println(Arrays.toString(a));
}
}
7. Exercise-Two-dimensional Array Sorting

Firstly, define a 5X8 two-dimensional array, and then fill it with random numbers.
Sort two-dimensional arrays with the help of Arrays.
Reference ideas:
First copy the two-dimensional array to a one-dimensional array using System.arraycopy.
Then use sort to sort.
Finally, copy back to the two-dimensional array.
About random numbers:
There are many ways to obtain random integers ranging from 0 to 100. The following is one of the reference methods:
(int) (Math. random() * 100)
Math. random() will get a random floating-point number between 0-1, then multiply it by 100 and force it to an integer.
The specific code is as follows:
import java.util.Arrays;
public class essay1{
public static void main(String[] args) {
int a[][] = new int[5][8];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = (int) (Math.random() * 100);
}
}
System.out.println(“Print a two-dimensional array”);
for (int[] i : a) {
System.out.println(Arrays.toString(i));
}
//Copy two-dimensional array to one-dimensional array
int b[] = new int[a.length * a[0].length];
for (int i = 0; i < a.length; i++) {
System.arraycopy(a[i], 0, b, i * a[i].length, a[i].length);
}
//Sort a one-dimensional array
Arrays.sort(b);
// Copy one-dimensional array to two-dimensional array
for (int i = 0; i < a.length; i++) {
System.arraycopy(b, a[i].length * i, a[i], 0, a[i].length);
}
System.out.println(“Print the sorted two-dimensional array:”);
for (int[] i : a) {
System.out.println(Arrays.toString(i));
}
}
}

The final output of the console:

8. Enhanced for Loop
Note: The enhanced for loop can only be used to obtain values, but not to modify the values in the array.
The specific code is as follows:
public class essay1{
public static void main(String[] args) {
int values [] = new int[]{18,62,68,82,65,9};
//Conventional traversal
for (int i = 0; i < values.length; i++) {
int each = values[i];
System.out.println(each);
}
//Enhanced for loop traversal
for (int each : values) {
System.out.println(each);
}
}
}

This is the learning content of the last chapter of arrays. After learning the concept of Arrays today, it will have a great impact on your array programming. The functions that previously required a few lines of code to achieve, after learning Arrays, only one line is needed. At the same time, the enhanced for loop learning content is also introduced in this chapter. I believe that through the learning of this chapter, your code will be more concise and effective.