In any programming language, array is one of the most important and commonly used data structures, but the structure and processing of array are different in different languages.
Java provides the java.util.Arrays class to operate Arrays conveniently, and all the methods it provides are static. Here are a few of the most commonly used methods of the Arrays class.
1. Array sorting
The Arrays utility class provides a sort method, which only needs one line of code to complete the sorting function.
2. Convert array to string
Arrays provides a toString method, which can directly convert an array into a string, so it is convenient to observe the elements in the array.
//Source: public number [time and bytes] //Array sorting and conversion to string package BaseCode; import java.util.Arrays; public class j4_1028_11 { public static void main(String[] args) { int[] ff= {11,3,25,71,9}; System.out.print("array ff Unsorted: "); for(int n:ff) System.out.print(n+" "); Arrays.sort(ff); // Sort arrays System.out.printf("\n array ff After sorting: "); for(int n:ff) System.out.print(n+" "); //Convert array to string System.out.printf("\n array ff To string: "+Arrays.toString(ff)); } }
Operation result
Array ff unsorted: 11 3 25 71 9 Array ff sorted: 3 9 11 25 71 Array ff to string: [3, 9, 11, 25, 71]
3. Filling and replacement of array elements
Arrays provides the fill method to fill or replace an array (or array specified location) with the specified value.
4. Judge whether the array is the same
Arrays.equals can compare whether the elements in two arrays are the same.
//Source: [time and bytes] //fill Method and equals Method package BaseCode; import java.util.Arrays; public class j4_1028_12 { public static void main(String[] args) { int[] ff= new int[5]; Arrays.fill(ff, 5); System.out.print("All elements of the array are filled with 5: "); for(int n:ff) System.out.print(n+" "); //Fill the array from the first element to the third element with 7 //With element 1, without element 3 Arrays.fill(ff,1,3,7); System.out.print("\n Array specified position is filled with 7: "); for(int n:ff) System.out.print(n+" "); int[] nn= new int[5]; Arrays.fill(nn, 5); System.out.printf("\nff And nn Same:"+Arrays.equals(ff, nn)); } }
Operation result
All elements of the array are filled with 5:5 5 5 5 5 Array specified position filled with 7:5 7 5 5 ff is the same as nn: false
5. Copy array
The copyOf() and copyRange() methods of the Arrays class can copy Arrays.
copyOf(arr, int newlength)
The parameter newlength is the length of the new array, that is, from the 0 th position of the array arr to the end of the newlength. If the newlength is greater than the length of the ARR, it will be filled with the default value.
copyOfRange(arr, int formIndex, int toIndex)
The parameter formIndex is the starting position of the element taken from the array arr, and toIndex is the ending position, but does not include the element at that position. For example, if toIndex exceeds the length of arr, it will be filled in by default.
//Source: public number [time and bytes] //Copy of array, copyOf And copyOfRange Use package BaseCode; import java.util.Arrays; public class j4_1028_10 { public static void main(String[] args) { int[] ff= {1,3,5,7,9}; //Arrays.copyOf Copy array to specified length, starting from 0 int[] newff1=Arrays.copyOf(ff, 3); int[] newff2=Arrays.copyOf(ff, 6); System.out.print("copyOf Use:\n array newff1: "); for(int n:newff1) System.out.print(n+" "); System.out.printf("\n array newff2: "); for(int n:newff2) System.out.print(n+" "); //Arrays.copyOfRange The second parameter is the start position //The third parameter is the end position int[] newff3=Arrays.copyOfRange(ff, 1, 4); int[] newff4=Arrays.copyOfRange(ff, 1, 7); System.out.printf("\ncopyOfRange Use:\n array newff3: "); for(int n:newff3) System.out.print(n+" "); System.out.printf("\n array newff4: "); for(int n:newff4) System.out.print(n+" "); } }
Operation result
Use of copyOf: Array newff1:1 3 5 Array newff2:1 3 5 7 9 0 Use of copyOfRange: Array newff3:3 5 7 Array newff4:3 5 7 9 0 0
6. Element query
The binarySearch method of the Arrays class can query the location of the element and return the index of the element. Note, however, that sort must be used before using binary search to find. And if there are more than one identical element in the array, the search result is uncertain.
binarySearch(arr, object key)
If the key is in the array, the index of the search value is returned; otherwise, the value of - 1 or negative insertion point is returned.
The so-called insertion point value is the index of the first element larger than key in the array, and the index starts from 1.
binarySearch(arr, int fromIndex, int endIndex, object key);
fromIndex: index at the beginning of the specified range (inclusive)
toIndex: index at the end of the specified range (not included)
Its search results can be divided into the following four situations:
-
If the search key is not in the range and is larger than the element in the range (array), return – (toIndex + 1);
-
If the search key is not in the range and is smaller than the element in the range (array), return – (fromIndex + 1);
-
The search key is in the range, but is not an array element. It starts counting from 1 and returns a negative index value of the insertion point;
-
The search key is in the range and is an array element. It starts counting from 0 and returns the index value of the search value;
See the following example code and notes
//Source: public number [time and bytes] //Find array elements: binarySearch Use of methods package BaseCode; import java.util.Arrays; public class j4_1028_13 { public static void main(String[] args) { int[] fn= {1,3,5,7,9}; Arrays.sort(fn);//Sort before finding int cx1=Arrays.binarySearch(fn,5);//Return to 2 ,Key found, index starts at 0 //6 not found, negative insertion point value returned, //6 The insertion point in the array is the index of element 7, //The index of element 7 is 4 from 1, all returned-4 int cx2=Arrays.binarySearch(fn,6);//Not found, return-4,The index of insertion point 7 is 4 int cx3=Arrays.binarySearch(fn,4);//Not found, return-3,The index of insertion point 5 is 3 System.out.println("Example of not specifying a lookup range:"); System.out.println("array fn Contents:"+Arrays.toString(fn)); System.out.printf("[5]Find: cx1=%d%n", cx1); System.out.printf("[6][4]Not found: cx2=%d, cx3=%d%n", cx2,cx3); //Finds an element in the specified position of an array,Parameter range (1,3)The array elements contained are[3,5] //The search key is not in range and is larger than the element in range (array), return –(toIndex + 1). int cx4=Arrays.binarySearch(fn,1,3,10); //The search key is not in the range and is smaller than the element in the range (array), return–(fromIndex + 1); int cx5=Arrays.binarySearch(fn,1,3,-3); //The search key is in the range, but not an array element, counted from 1, and returns a negative insertion point index value int cx6=Arrays.binarySearch(fn,1,3,4); //The search key is in the range and is an array element, counting from 0 and returning the index value of the search value int cx7=Arrays.binarySearch(fn,1,3,5); System.out.println("-------------------------"); System.out.println("Example of specifying a lookup range with parameters:"); System.out.println("The first situation: cx4= "+cx4); System.out.println("The second situation: cx5= "+cx5); System.out.println("The third situation: cx6= "+cx6); System.out.println("The fourth situation: cx7= "+cx7); } }
Operation result
Example of not specifying a lookup range: Contents of array fn: [1, 3, 5, 7, 9] [5] find: cx1=2 [6] [4] not found: cx2=-4, cx3=-3 ------------------------- Example of specifying a lookup range with parameters: The first case: cx4= -4 The second case: cx5= -2 The third case: cx6= -3 Case 4: cx7= 2