Java array 01 (basic definition and use of array)

The definition of
an array 1. An array is an ordered collection of the same type
2. An array describes a number of data of the same type, sorted and combined in a certain order
3. Among them, each data becomes an array element, and each array Elements can be accessed through a subscript.
Array declaration and creation
1. The array variable must be declared first before the array can be used in the program. The following is the syntax for declaring array variables:

dataType[] arrayRefVar;//首先方法
或
dataType arrayRefVar[];//效果相同,但不是首选方法

2. Java language uses the new operator to create an array, the syntax is as follows:

dataType[] arrayReVar = new dataType[arraySize];

3. The elements of the array are accessed by index, and the array index starts from 0.
4. Get the length of the array:

arrays.length

Code example

package com.hao.array;

public class ArrayDemo01 {
    public static void main(String[] args) {
        int [] numbers ;//定义(首选)
        numbers =new int[10];
        for (int i = 0; i < numbers.length; i++) {
            numbers[i]=i+1;
        }
        for (int i = 0; i < numbers.length; i++) {
            System.out.println(numbers[i]);
        }
    }
}

Output example
Insert picture description here
three kinds of initialization
1. Static initialization

int[] a={1,2,3};
Man[] mans ={new Man(1,1),new Man(2,2)};

2. Dynamic initialization

int[] a = new int [2];
a[0] = 1;
a[1] = 2;

Your favorite way of defining arrays:

数组类型 [] 数组名=new 数组类型[数组元素个数];

Four basic characteristics of the array
1. Its length is determined. Once the array is created, its size cannot be changed.
2. The elements must be of the same type, and mixed types are not allowed.
3. The elements in the array can be any data type, including basic types and reference types.
4. Array variables are reference types, and arrays can also be regarded as objects. Each element in the array is equivalent to a member variable of the object. The array itself is an object, and the object in Java is in the heap. Therefore, whether the array saves the original type or other object types, the array object itself is in the heap.
Summary:
1. An array is an ordered collection of the same data type
2. An array is also an object, and the elements of the array are equivalent to the member variables of the object
3. The length of the array is determined, and an example
of using code for an immutable array

package com.hao.array;

public class ArrayDemo02 {
    public static void main(String[] args) {
        int[] arrays ={1,2,3,4,5};
        //遍历数组
        for (int array : arrays) {
            System.out.println(array);
        }
    }

}

Output sample
Insert picture description here
code example

package com.hao.array;

public class ArrayDemo03 {
    public static void main(String[] args) {
        int [] arrays={1,2,3,4,5};
        int[] reverse = reverse(arrays);
        reverse(arrays);
        printArray(reverse);

    }
    public static int[] reverse(int [] arrays ){
        int[] result =new int [arrays.length];
        //反转操作
        for (int i = 0,j=arrays.length-1; i < arrays.length; i++,j--) {
            result[j]=arrays[i];
        }
        return result;
    }
    //遍历数组
    public static void printArray(int[] arrays){
        for (int i = 0; i < arrays.length; i++) {
            System.out.println(arrays[i]);
        }
    }

}

Sample output
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_51224492/article/details/111397776