Summary of Java knowledge points [2] Array

table of Contents

 

1. The role of arrays

2. Array creation

3. The use of arrays

4. Array as a method parameter

5. Array as the return value of the method

6. Array related exercises

①Integer array to string

②Dichotomy finds an element in an ordered array

③ Array reverse order

④Check the order of the array

⑤ Array copy


1. The role of arrays

Allows us to create variables of the same type in batches

2. Array creation

The following three creation methods are all possible

int[] arr1=new int[] {1,2,3,4};
int[] arr2={1,2,3,4};
int[] arr3=new int[4];

3. The use of arrays

①Get the length (use .length)

public class Pra0104 {
    public static void main(String[] args) {
        int[] arr={1,2,3,4};
        System.out.println(arr.length);   
    }
}

②Access elements

By removing the standard access element in the array in such a manner, the note can not be out of range index.

for loop to traverse the array

public class Pra0104 {
    public static void main(String[] args) {
        int[] arr={1,2,3,4};
        for(int i=0;i<arr.length;i++){
            System.out.println(arr[i]);
        }
    }
}

for each traverse the array , as follows

public class Pra0104 {
    public static void main(String[] args) {
        int[] arr={1,2,3,4};
        for(int x:arr){
            System.out.println(x);
        }
    }
}

operation result:

4. Array as a method parameter

For example:

public class Pra0104 {
    public static void main(String[] args) {
        int[] arr={1,2,3,4};
        printArr(arr);

    }
    public static void printArr(int[] arr) {
        for(int x:arr){
            System.out.println(x);
        }
    }
}

operation result:

5. Array as the return value of the method

Example: Multiply each element in the array by 2

public class Pra0104 {
    public static void main(String[] args) {
        int[] arr1={1,2,3,4};
        int[] arr2=transform(arr1);
        for(int x:arr2){
            System.out.println(x);
        }

    }

    public static int[] transform(int[] arr1) {
        int[] arr2=new int[arr1.length];
        for(int i=0;i< arr1.length;i++){
            arr2[i]=arr1[i]*2;
        }
        return arr2;
    }

}

operation result:

6. Array related exercises

①Integer array to string

Code

import java.util.Arrays;
public class Pra0104 {
    public static void main(String[] args) {
        int[] arr={1,2,3,4};
        String str= Arrays.toString(arr);
        System.out.println(str);
    }
}

operation result

②Dichotomy finds an element in an ordered array

Specific ideas, written in the previous C language column

import java.util.Scanner;

public class Pra0104 {
    public static void main(String[] args) {
        int[] arr={2,4,6,8,10};
        find(arr);
    }

    public static void find(int[] arr) {
        System.out.println("请输入要查找的数字");
        Scanner sc=new Scanner(System.in);
        int find=sc.nextInt();
        int left=0;
        int right=arr.length-1;
        while(left<=right){
            int mid=(left+right)/2;
            if(find<arr[mid]){
                right=mid-1;
            }else if(find>arr[mid]){
                left=mid+1;
            }else{
                System.out.println("找到了!下标是:"+mid);
                break;
            }
        }
        if(left>right){
            System.out.println("找不到!");
        }
    }
}

operation result:

③ Array reverse order

Set two subscripts, left and right

public class Pra0104 {
    public static void main(String[] args) {
        int arr[]={1,2,3,4};
        reverseArr(arr);
        //输出逆序后的数组
        for(int x:arr){
            System.out.println(x);
        }
    }

    public static void reverseArr(int[] arr) {
        int left=0;
        int right=arr.length-1;
        while(left<right){
            int temp=arr[left];
            arr[left]=arr[right];
            arr[right]=temp;
            left++;
            right--;
        }
    }
}

operation result:

④Check the order of the array

Here to check whether the array is in ascending order, return true or false

public class Pra0104 {
    public static void main(String[] args) {
        int[] arr={1,2,3,4};
        System.out.println(isOrder(arr));
    }

    public static boolean isOrder(int[] arr) {
        for(int i=0;i<arr.length-1;i++){
            if(arr[i]>arr[i+1]){
                return false;
            }
        }
        return true;
    }
}

operation result:

⑤ Array copy

Implement your own copy method

public class Pra0104 {
    public static void main(String[] args) {
        int[] arr={1,2,3,4};
        int[] newArr=copy(arr);
        for(int x:newArr){
            System.out.println(x);
        }

    }

    public static int[] copy(int[] arr) {
        int[] ret=new int[arr.length];
        for(int i=0;i<arr.length;i++){
            ret[i]=arr[i];
        }
        return ret;
    }
}

operation result:

Use the library

import java.util.Arrays;
public class Pra0104 {
    public static void main(String[] args) {
        int[] arr={1,2,3,4};
        int[] newArr=Arrays.copyOf(arr,arr.length);
        System.out.println(Arrays.toString(newArr));
    }
}

operation result:

Note: When copying, there may be questions, why not just int[] newArr=arr? This is because this direct assignment is equivalent to another alias to the original array, newArr points to the array pointed to by arr, so the actual array is still the same, and the copy effect is not achieved, as shown in the figure below

Guess you like

Origin blog.csdn.net/weixin_43939602/article/details/112325394