Three ways to declare arrays, traversal of multidimensional arrays, and summary of common methods of the Arrays class

1. Three ways to declare an array

public static void main(String[] args) {
    //第一种
    int[] a=new int[3];
    a[0]=1;
    a[1]=2;
    //第二种
    int[] b={1,2,3};
    //第三种
    int[] c=new int[]{1,2,4};
    
    int [][] a=new int[2][]{
   
   {1,2},{3,4,5}};//这种赋值方法是错误的
    int [2][] a=new int[][]{
   
   {1,2},{3,4,5}};//这种赋值方法是错误的
    int [2][] a=new int[2][]{
   
   {1,2},{3,4,5}};//这种赋值方法是错误的

    //这种是可以的
    int [][] b=new int[2][];
    b[0]=new int[3];
    b[1]=new int[4];

    score2=new int[][]{
   
   {1,2,3},{3,4,5,6},{16,7}};//静态初始化
}

2. Traversal of multidimensional arrays

score2=new int[][]{
   
   {1,2,3},{3,4,5,6},{16,7}};//静态初始化
for (int i = 0; i < score2.length; i++) {
    for (int j = 0; j < score2[i].length; j++) {
        System.out.print(score2[i][j]+" ");
    }
    System.out.println();
}

Three. Common methods of Arrays class

1. Fill the array: fill

int[] a=new int[3];
Arrays.fill(a,2);
for (int i = 0; i <a.length ; i++) {
    System.out.print(a[i]+" ");
}

Result: 2 2 2
Analysis: Assign 2 to all values

int[] a=new int[5];
Arrays.fill(a,1,3,8);
for (int i = 0; i <a.length ; i++) {
    System.out.print(a[i]+" ");
}

The result is: 0 8 8 0 0
Analysis: Assign 8 to bit 1 (0-based) to bit 3 (exclusive)

2. Array element sorting: sort

int[] a=new int[]{3,2,5,4,1};
Arrays.sort(a);
for (int i = 0; i <a.length ; i++) {
    System.out.print(a[i]+" ");
}

The result is: 1 2 3 4 5
Analysis: Sort all numbers in ascending order

int[] a=new int[]{3,2,5,4,1};
Arrays.sort(a,1,4);
for (int i = 0; i <a.length ; i++) {
    System.out.print(a[i]+" ");
}

The result is: 3 2 4 5 1

Analysis: Sort 1st (0-based) to 4th (exclusive)

3. Compare array elements for equality: equals

int[] a=new int[]{3,2,5,4,1};
int[] b=new int[]{3,2,5,4,1};
System.out.println(Arrays.equals(a,b));

The result is: true

int[] a=new int[]{3,2,5,4,1};
int[] b=new int[]{3,2,5,4,1};
System.out.println(Arrays.equals(a,b));

The result is: false
If it is arr1.equals(arr2), it will return false, because equals compares the addresses of the two objects, not the numbers inside, and Arrays.equals overrides equals, so here you can compare whether the elements are equal .

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

int []arr = {10,20,30,40,50};
System.out.println(Arrays.binarySearch(arr, 20));

The result is: 1
Analysis: The element can be found, and the return subscript is 1 (starting from 0)

int []arr = {10,20,30,40,50};
System.out.println(Arrays.binarySearch(arr, 35));

The result is: -4
Analysis: No element found, return -x, start counting from -1, if the title, return -4

int []arr = {10,20,30,40,50};
System.out.println(Arrays.binarySearch(arr, 0,3,30));

The result is: 2
Analysis: find 30 from 0 to 3 digits (exclusive), found it, at 2, return 2

int []arr = {10,20,30,40,50};
int []arr1 = Arrays.copyOf(arr, 3);
output(arr1);

Result: 10 20 30
Analysis: Intercept 3 elements of the arr array and assign them to the surname array arr1

int []arr = {10,20,30,40,50};
int []arr1 = Arrays.copyOfRange(arr,1,3);
output(arr1);

Result: 20 30
Analysis: Intercepted from 1st position (0-based) to 3rd position (excluding)
[1] https://blog.csdn.net/liu_yanzhao/article/details/70847050
[2] http:// www.runoob.com/java/java-array.html
[3] http://baijiahao.baidu.com/s?id=1601084106055683243&wfr=spider&for=pc

Guess you like

Origin blog.csdn.net/MCYZSF/article/details/88918046