Basic usage of arrays class in Java
First knowledge of Java arrays class
The arrays class includes many static methods (such as sorting and searching) for manipulating arrays, and the static methods can be called directly through the class name arrays. Before using, you need to import the arrays class:
import java.util.Arrays;
This chapter records several basic operations on basic data types, which will be supplemented later after technical improvement.
Binary search
int[] a = {1,2,3,4};
System.out.println(Arrays.binarySearch(a,1));//输出0 即1的索引值
System.out.println(Arrays.binarySearch(a,7));//输出-1 找不到就负数
The binary method finds the index value of the key in the a array. If it cannot be found, it returns a negative number; The array needs to be arranged in ascending order.
int[] b = {5,6,7,8};
System.out.println(Arrays.binarySearch(b,6));//返回1 b中0-3里有6就返回6的索引
System.out.println(Arrays.binarySearch(b,9));//返回负数 b中0-3里没有9,返回负数
System.out.println(Arrays.binarySearch(b,4,8));//返回3 不包括索引为4的值
System.out.println(Arrays.binarySearch(b,5,8));//报错,超出范围
Similar to method 1, but when searching the index value from fromindex to toindex (excluding toindex), the index range needs to be noted.
Array copy
int[] a = {1,4};
int[] c = Arrays.copyOf(a,5);
int[] d = Arrays.copyOf(a,3);
for(int m:d) System.out.print(m);//123
for(int m:c) System.out.print(m);//12340
String[] b = Arrays.copyOf(a,4)//报错,类型不符
Copy the original array into a new array. Length is the length of the new array. There are three cases:
int[] a ={1,5};
int[] b =Arrays.copyOfRange(a,1,3);
int[] c = Arrays.copyOfRange(a,6);
System.out.println(Arrays.toString(b));//[2,3]
System.out.println(Arrays.toString(c));//[1,0]
Similarly, only the elements from the from index of the original array to the to index are copied.
Convert to string
char[]b = {'a','b','c'};
System.out.println(Arrays.toString(b));//[a,b,c]
Convert the array to a string type, and separate the array elements in order with commas and spaces.
Array fill
String[] stringArray = new String[3];
Arrays.fill(stringArray,"he");
System.out.println(Arrays.toString(stringArray));//[he,he,he]
Fill all elements of the a array with val
String[] stringArray = {"he","he","he"};
Arrays.fill(stringArray,"m");
System.out.println(Arrays.toString(stringArray));//[m,he]
Fill the elements from fromindex to toindex with Val until the previous number of toindex (excluding toindex).
Array comparison
int[] a = {5,8};
int[] b = {5,8};
int[] c = {5,7};
System.out.println(Arrays.equals(a,b));//true
System.out.println(Arrays.equals(a,c));//false
If the lengths of array A and array A2 are equal and the elements inside are the same, return true.
Array sorting
char[] d = {'d','a','c','b'};
Arrays.sort(d);
System.out.println(Arrays.toString(d));//[a,c,d]
Arrange the original array in ascending order.
int[] a ={3,4};
Arrays.sort(a,2);
System.out.println(Arrays.toString(a));//[1,4]
Arrange the original array in ascending order from fromindex to toindex until the number before toindex (excluding toindex).
To sum up: