The Arrays class in Java (2,000 words in detail)

Common methods of the Arrays class

  • Arrays class: This class contains various methods for manipulating arrays (such as sorting and searching). The methods in this class are all static methods, and the array can be treated as a list. Methods in this class throw a NullPointerException if the specified array reference is null, unless otherwise specified.

(1) toString() method

(1) toString() method: returns the string representation of the contents of the specified array.


	Integer[] integers = {
    
    1, 20, 90};
	System.out.println(Arrays.toString(integers));

(2) binarySearch() method

(2) binarySearch() method: use the binary search algorithm to search for the specified value in the specified array, the array must be in ascending order, and the return value is the index of the specified value; if the specified value does not exist, return -(low + 1) .


	Integer[] arr = {
    
    1, 2, 90, 123, 567};
	int index = Arrays.binarySearch(arr, 567);
    System.out.println("index = " + index);
    

(3) copyOf() method

(3) copyOf() method: Copies the specified array, truncating or padding (if necessary) with null values ​​so that the copy has the specified length.


	/*
    1. 从 arr 数组中,拷贝 arr.length个元素到 newArr数组中
    2. 如果拷贝的长度 > arr.length 就在新数组的后面 增加 null
    3. 如果拷贝长度 < 0 就抛出异常NegativeArraySizeException
    4. 该方法的底层使用的是 System.arraycopy()*/
	Integer[] arr = {
    
    1, 2, 90, 123, 567};
    Integer[] newArr = Arrays.copyOf(arr, arr.length);
    System.out.println("==拷贝执行完毕后==");
    System.out.println(Arrays.toString(newArr));
    

(4) equals() method

(4) equals() method: returns true if the two specified object arrays are equal to each other. Two arrays are considered equal if they both contain the same number of elements or are both null, and all corresponding pairs of elements in both arrays are equal.


	Integer[] arr = {
    
    1, 2, 90, 123, 567};
	Integer[] arr2 = {
    
    1, 2, 90, 123};
	boolean equals = Arrays.equals(arr, arr2);
	System.out.println("equals=" + equals);
	

(5) fill() method

(5) fill() method: Assign the specified value to each element of the specified array.


	Integer[] num = new Integer[]{
    
    9,3,2};
	Arrays.fill(num, 99);
    System.out.println("==num数组填充后==");
    System.out.println(Arrays.toString(num));
    

(6) Ordinary sort() method

(6) Ordinary sort() method: sort the specified array, the default is ascending.


	Integer arr[] = {
    
    1, -1, 7, 0, 89};
	Arrays.sort(arr); // 默认排序方法
	tem.out.println("===排序后===");
    System.out.println(Arrays.toString(arr));
    

(7) Customized sort() method

(7) Customized sort(T[] a, Comparator<? super T> c) method: Sorts the specified array of objects according to the order induced by the specified comparator. All elements in the array must be compared to each other by the specified comparator.

  • It embodies the comprehensive use of interface programming + dynamic binding + anonymous internal classes, which will be very common in the use of underlying frameworks and source codes in the future.

public class ArraysSortCustom {
    
    
    public static void main(String[] args) {
    
    

        int[] arr = {
    
    1, -1, 8, 0, 20};
        
        // 传入我们需要排序的数据,和实现了 Comparator 接口的类的对象
        // Comparator 接口的类的对象 相当于我们想要排序的规则,通过匿名内部类我们可以轻松地更改这个规则。
        bubble(arr, new Comparator() {
    
    // 使用了匿名内部类
        
        	// 重写了接口中的 compare() 方法;
            @Override
            public int compare(Object o1, Object o2) {
    
    
                int i1 = (Integer) o1;// 向下转型
                int i2 = (Integer) o2;
                return i2 - i1;// 或者 return i1 - i2;会决定最终是升序还是降序排序
            }
        });

        System.out.println("==定制排序后的情况==");
        System.out.println(Arrays.toString(arr));

    }

    // 定制排序方法,此处我们使用冒泡;
    // Arrays.sort() 源码使用的是 二叉排序。
    public static void bubble(int[] arr, Comparator c) {
    
    
        int temp = 0;
        for (int i = 0; i < arr.length - 1; i++) {
    
    
            for (int j = 0; j < arr.length - 1 - i; j++) {
    
    
            
                // 调用接口中的 compare() 方法,我们可以重写该方法;
                // 最终数组顺序由 c.compare(arr[j], arr[j + 1]) 返回的值决定
                if (c.compare(arr[j], arr[j + 1]) > 0) {
    
    
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
}

Summarize:

  • This article is the learning notes compiled and summarized by Xiaobai blogger when he was studying the Java online class of teacher Han Shunping at station B. Here, I would like to thank teacher Han Shunping for his online class. If you are interested, you can also go and have a look.
  • This article explains the concept and use of the Arrays class in detail, and also gives many examples. I hope you can gain something after reading it!
  • Finally, if there are any mistakes or omissions in this article, everyone is welcome to criticize and correct! work hard together! ! See you in the next blog post!

Guess you like

Origin blog.csdn.net/weixin_45395059/article/details/125786507