Click to enter _ more _java thousand ask
1. What is the function of arrays class?
The classes in Java.util.Arrays contain a number of static methods for sorting arrays, searching arrays, comparing arrays, and populating array elements.
Understanding a one-dimensional array look here: What is an array in Java
The common methods are as follows:
public static int BinarySearch (object[] A, Object key)
Searches for the position (subscript) of the specified value in the array using the dichotomy method. If the array type is not a basic data type, the class is required to implement the CompareTo method in the comparable interface.
public static Boolean Equals (Long[] A, long[] A2)
Compares two arrays for equality. Returns true if two of the specified arrays are equal. The default method for determining the equality of two arrays is:
- Two arrays contain the same number of elements, and all elements of the two array correspond to equal.
- The same method can be used for all other data types (byte, short, int, etc.)
public static void Fill (int[] A, int val)
Fills all the elements of the array with the specified int value. The same method can be used for all other raw data types (byte, short, int, and so on).
There is also a method for specifying the fill location: public static void Fill (byte[] A, int fromIndex, int toindex, byte val), you can specify some elements to populate.
4.public static String toString (object[] a)
The values of each element are assembled sequentially into a string of type strings, and the same method can be used for all other data types (byte, short, int, etc.)
- public static void sort (object[] a)
The specified array is sorted in ascending order according to the natural ordering of its elements, and the same method can be used for all other data types (byte, short, int, etc.)
2. How to use arrays
The following examples show: BinarySearch, CopyOf, Copyofrange, equals, fill, sort, tostring and other methods. As follows:
Import Java.util.Arrays; Public classtestarrays { Public Static void Main(string[] args) {int[] A = {3,4,5,6};int[] B = {3,4,5,6}; System. out. println ("A and B are equal:"+ Arrays.equals (A, b));//trueSystem. out. println ("5 position in a:"+ Arrays.binarysearch (A,5));//2 int[] C = arrays.copyof (A,6); System. out. println ("A and C are equal:"+ Arrays.equals (A, c));//falseSystem. out. println ("element of C:"+ arrays.tostring (c));//3,4,5,6,0,0Arrays.fill (c,2,4,1);//assigns the 3rd to 5th Element (not included) in C to a value of 1System. out. println ("element of C:"+ arrays.tostring (c));//3,4,1,1,0,0Arrays.sort (c); System. out. println ("element of C:"+ arrays.tostring (c));//0,0,1,1,3,4}}
The results of the implementation are as follows:
A and B are equal: true
5 position in a: 2
A and C are equal: false
Elements of C: [3, 4, 5, 6, 0, 0]
Elements of C: [3, 4, 1, 1, 0, 0]
Elements of C: [0, 0, 1, 1, 3, 4]
Java thousand ask _06 data structure (019) _arrays class have what function