Java array declaration creation assignment length

Array declaration and creation

Array declaration

dataType[] arrayRefaVar;//首选

dataType arrayRefaVar[];

Create array

Use new to create array variables

dataType[] arrayRefaVar = new dataType[arraySize];

The length of the array

Using the length property of the array, each array has a length property.
Such as: get the length of the array named array

int length = array.length

Initialization of the array

Static initialization

The length of the array is not specified, the initial value is assigned at the time of declaration, and the length is automatically declared by the system

int[] array=1,2,3,4,5;

ObejcetType[] objects={
    
    new ObjectType(2),new ObjectType(2)};

Dynamic initialization

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

Default initialization

The array is a reference type , and its element type is equivalent to the instance variable of the class. Once space is allocated for the array, each element in it is implicitly initialized in the same way as the instance variable.

Guess you like

Origin blog.csdn.net/qq_36976201/article/details/112060710