Home > Java > javaTutorial > Java Arrays class usage example analysis

Java Arrays class usage example analysis

WBOY
Release: 2023-05-12 08:16:14
forward
967 people have browsed it

1.toString method

Outputs the array as a string according to the default format.

public static void main(String[] args) {
    int dataA[] = new int[] {1,2,5,4,3};
    System.out.println(Arrays.toString(dataA));
}
Copy after login

2.sort method

Sort the array in ascending order.

Note

Parameter array analysis

(1) is a numerical value, the default is ascending order

(2) is a string (English) in ascending alphabetical order Sorting

(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, then the custom class must have a comparable or Comparator interface Support

int[] intTest={15,78,32,5,29,22,17,34};
Arrays.sort(intTest);
output(intTest);
Copy after login

3.equals

Compares whether array elements are equal.

int []arr1 = {1,2,3};
int []arr2 = {1,2,3};
System.out.println(Arrays.equals(arr1,arr2));
Copy after login

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 is the detailed content of Java Arrays class usage example analysis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template