Java array key notes

Array definition and reference: (reference data type)

1. Array: A collection that stores the same data type

2. There are two ways to create an array:

Static initialization:

int[] =arr={1,3,5};

Dynamic initialization:

int[] arr=new int[3];

int[] arr=new int[3]{1,3,5};

3. Data access and length:

Get the length of an array array name.length

Get array element length arr[index]->index (index->offset)

4. Traversal of the array:

Traversal: Access all elements in the collection in a certain order

fori loop

public class Test43 {
    public static void main(String[] args) {
        int[] arr={1,3,5};
        for (int i = 0; (i < arr.length); i++) {
            System.out.print(arr[i]+" ");
        }
    }
}

traverse

public class Test43 {
    public static void main(String[] args) {
        int[] arr={1,3,5};
        for(int i:arr){
            System.out.print(i+" ");
        }
    }
}

heap: objects created by new

stack: local variables

Address: An identifier for locating memory

Reference: It is equivalent to naming the existing entity, and the reference data type stores an address value

Guess you like

Origin blog.csdn.net/m0_62218217/article/details/121444360