[Java Basics] Methods provided by the Arrays class

table of Contents

 

Foreword:

Array copy

Convert to string

Sort

search for

Judge whether they are the same

filling


Preface

Arrays is a tool class for arrays, which can perform functions such as sorting, searching, copying and filling. Greatly improve the work efficiency of developers.

 

Array copy

Similar to System.arraycopy();, Arrays provides a copyOfRange method for array copying.

The difference is System.arraycopy, which needs to prepare the target array in advance and allocate the length. copyOfRange only needs the source array, and through the return value, you can get the target array.

In addition, it should be noted that the third parameter of copyOfRange, which represents the end position of the source array, is not available.

import java.util.Arrays;
 
public class HelloWorld {
    public static void main(String[] args) {
        int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
 
        // copyOfRange(int[] original, int from, int to)
        // 第一个参数表示源数组
        // 第二个参数表示开始位置(取得到)
        // 第三个参数表示结束位置(取不到)
        int[] b = Arrays.copyOfRange(a, 0, 3);
 
        for (int i = 0; i < b.length; i++) {
            System.out.print(b[i] + " ");
        }
 
    }
}

 

Convert to string

If you want to print the contents of an array, you need to traverse one by one through a for loop, printing one by one

But Arrays provides a toString() method, which directly converts an array into a string, so that it is convenient to observe the contents of the array

import java.util.Arrays;
  
public class HelloWorld {
    public static void main(String[] args) {
        int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
        String content = Arrays.toString(a);
        System.out.println(content);
  
    }
}

 

Sort

The Arrays tool class provides a sort method, which only requires one line of code to complete the sort function.

import java.util.Arrays;
  
public class HelloWorld {
    public static void main(String[] args) {
        int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
        System.out.println("排序之前 :");
        System.out.println(Arrays.toString(a));
        Arrays.sort(a);
        System.out.println("排序之后:");
        System.out.println(Arrays.toString(a));
  
    }
}

 

search for

Query where the element appears. It
should be noted that before using binarySearch to search, you must use sort to sort.
If there are multiple identical elements in the array, the search result is uncertain

import java.util.Arrays;
 
public class HelloWorld {
    public static void main(String[] args) {
        int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
 
        Arrays.sort(a);
 
        System.out.println(Arrays.toString(a));
        //使用binarySearch之前,必须先使用sort进行排序
        System.out.println("数字 62出现的位置:"+Arrays.binarySearch(a, 62));
    }
}

 

Judge whether they are the same

Compare whether the contents of the two arrays are the same
. The last element of the second array is 8, which is different from the first array, so the comparison result is false

 

filling

Use the same value to fill the entire array

import java.util.Arrays;
  
public class HelloWorld {
    public static void main(String[] args) {
        int a[] = new int[10];
  
        Arrays.fill(a, 5);
  
        System.out.println(Arrays.toString(a));
  
    }
}

 

Guess you like

Origin blog.csdn.net/qq_44624536/article/details/113525713