Java provides a tool class specifically for operating arrays, the Arrays class , located in the java.util package. The Arrays class contains various methods for manipulating arrays, such as sorting and searching. In addition to this, the Arrays class also contains a static factory that allows arrays to be treated as lists. The methods in the Arrays class are all static methods, and we can call them directly through Arrays.methodname() .
Commonly used methods of the Arrays class are as follows:
This method is a method to convert an array into a List collection.
List<String>list=Arrays.asList(a,b,c);
Notice:
1) This method is suitable for arrays of object data (String, Integer,...).
2) This method is not recommended for use with arrays of basic data types (byte, short, int, long, float, double, boolean).
3) This method links the array and the List. When one of them is updated, the other one is automatically updated.
4) Methods such as add(), remove(), clear() are not supported.
The public static void fill(Object[] a,int fromIndex,int toIndex,Object val) in the Java API assigns the specified Object reference to each element in the specified range of the specified Object array. The filled range starts from the index fromIndex (including ) all the way to index toIndex (exclusive), if fromIndex==toIndex, the fill range is empty.
1)Arrays.copyOf()
The array returned by this method is a new array object. Changing the element values in the returned array will not affect the original array. The first variable represents the original array object, and the second variable represents the length of the new array. If the length of the new array exceeds the length of the original array, the default basic type values of the array elements are retained.
2)Arrays.copyOfRange()
This method is used to intercept and copy an existing array, and copy an array with a closed left range and an open right range. Copy an original array starting from the subscript from to the superscript to, and generate a new array and return it.
Note : From is included here, but to is not included, that is, [from, to).
This method is used to compare and determine whether two array elements are equal, for example:
importjava.util.*;publicclassMain{publicstaticvoidmain(String[]args){int[]arr1={9,5,1,33};int[]arr2={9,5,1,33};System.out. println(arr1==arr2:+(arr1==arr2));//Compare System.out.println(Arrays.equals(arr1,arr2):+Arrays.equals(arr1,arr2));}}
The running results are as follows:
arr1==arr2:falseArrays.equals(arr1,arr2):true
This method is a static method of the Arrays class, used to sort arrays. The time complexity is O(n*logn), and the method return value is void. After sorting, the sorted results are stored in the array.
Searches the specified element in the sorted array through binary method and returns the subscript of the element. If the element exists in the array, return the subscript of the element in the array; if the element does not exist in the array, return - (insertion point + 1), where the insertion point refers to, if the element exists In an array, the index of the element in the array.