Common Methods of Arrays Class in Java

The Arrays class is located in the java.util package and mainly includes various methods for manipulating arrays.

import java.util.Arrays;

Create a List: Arrays.asList()

List<String> list = Arrays.asList("zhangsan", "lisi", "wangwu", "sunliu");

Fill the array: Arrays.fill()

int[] arr = new int[5];//新建一个大小为5的数组
Arrays.fill(arr,4);//给所有值赋值4
int[] arr = new int[5];//新建一个大小为5的数组
Arrays.fill(arr,2,4,6);//给第2位(0开始)到第4位(不包括)赋值6

Array sorting: Arrays.sort()

The Arrays class has a static method sort. Using this method, the array to be sorted can be passed in for sorting. Because the passed in is a reference to an array, the result of sorting is also changed through this reference to change the array.

1. Number sorting

 int[] intArray = new int[] {
    
     4, 1, 3, -23 };
Arrays.sort(intArray);

2. String sorting, first uppercase and then lowercase

String[] strArray = new String[] {
    
     “z”, “a”,C};
Arrays.sort(strArray);
//输出: [C, a, z]
// 严格按字母表顺序排序,也就是忽略大小写排序 Case-insensitive sort
Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);
//输出: [a, C, z]

3. Reverse sort, Reverse-order sort

Arrays.sort(strArray, Collections.reverseOrder());
//输出:[z, a, C]
// 忽略大小写反向排序 Case-insensitive reverse-order sort
Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);
Collections.reverse(Arrays.asList(strArray));
//输出: [z, C, a]

4. Select the specified position of the array for sorting

int[] arr = {
    
    3,2,1,5,4};
Arrays.sort(arr,0,3);//给第0位(0开始)到第3位(不包括)排序
//输出:[1, 2, 3, 5, 4]

Print out all the contents of the array: Arrays.toString()

String str = Arrays.toString(arr); // Arrays类的toString()方法能将数组中的内容全部打印出来
//System.out.print(str);
//输出:[3, 2, 1, 5, 4]

Compare array elements for equality: Arrays.equals()

int[] arr1 = {
    
    1,2,3};
int[] arr2 = {
    
    1,2,3};
System.out.println(Arrays.equals(arr1,arr2));
//输出:true
//如果是arr1.equals(arr2),则返回false,因为equals比较的是两个对象的地址,不是里面的数,而Arrays.equals重写了equals,所以,这里能比较元素是否相等。

Binary search method to find the index value of the specified element: Arrays.binarySearch()

The array must be sorted, otherwise an error will occur. element found, only the last position will be returned

int[] arr = {
    
    10,20,30,40,50};
System.out.println(Arrays.binarySearch(arr, 30));
//输出:2 (下标索引值从0开始)

int[] arr = {
    
    10,20,30,40,50};
System.out.println(Arrays.binarySearch(arr, 36));
//输出:-4 (找不到元素,返回-x,从-1开始数,如题,返回-4)

int []arr = {
    
    10,20,30,40,50};
System.out.println(Arrays.binarySearch(arr, 0,3,30));
//输出:2 (从0到3位(不包括)找30,找到了,在第2位,返回2)

int []arr = {
    
    10,20,30,40,50};
System.out.println(Arrays.binarySearch(arr, 0,3,40));
//输出:-4 (从0到3位(不包括)找40,找不到,从-1开始数,返回-4)

Intercepting arrays: Arrays.copeOf() and Arrays.copeOfRange()

int[] arr = {
    
    10,20,30,40,50};
int[] arr1 = Arrays.copyOf(arr, 3);
//输出:[10, 20, 30] (截取arr数组的3个元素赋值给新数组arr1)

int []arr = {
    
    10,20,30,40,50};
int []arr1 = Arrays.copyOfRange(arr,1,3);;
//输出:[20, 30] (从第1位(0开始)截取到第3位(不包括))

Guess you like

Origin blog.csdn.net/twi_twi/article/details/130020483