There are several common operations in arrays that everyone needs to master, because they will be frequently used when learning java arrays. There are some things that you have been exposed to before, you can review them again to deepen your memory of such usage. This article summarizes three methods for you: toString, sort and equals. After a simple analysis, the corresponding code examples will be displayed.
1.toString method
Output the array as a string in the default format.
public static void main(String[] args) { int dataA[] = new int[] {1,2,5,4,3}; System.out.println(Arrays.toString(dataA)); }
2.sort method
Sort the array in ascending order.
Notice
Parameter array analysis
( 1) is a numerical value, sorted in ascending order by default
( 2) It is a string (English) sorted in ascending alphabetical order
( 3) It is a string (Chinese) sorted in ascending order according to the encoding number of the string
( 4) If it is a custom type, the custom class must be supported by the comparable or Comparator interface
int[] intTest={15,78,32,5,29,22,17,34}; Arrays.sort(intTest); output(intTest);
3.equals
Compare array elements for equality.
int []arr1 = {1,2,3}; int []arr2 = {1,2,3}; System.out.println(Arrays.equals(arr1,arr2));
Note: If the element values of two arrays are the same, but the corresponding position elements of the two arrays are different, the return result of Arrays.equals is false.
The above are the common uses of the Arrays class in Java. After you learn it, you can save it for later review. Of course, the usage of the Arrays class does not end there, and you need to find more methods to supplement it after class.