Summary of several ways to create arrays in Java

1. Declaration method of one-dimensional array:

type[] arrayName; Or type arrayname [];

Note: the first format is recommended because it has better readability, indicating that type [] is a reference type (array) rather than a type. It is not recommended to use the second format

The following is a typical way to declare an array:

Note: when declaring an array in the Java language, its length (the number of elements in the array) cannot be specified. This is because the array is a variable of reference type. Therefore, when using it to define a variable, it only means that a reference variable is defined (that is, a pointer is specified). This reference variable does not point to any valid memory, so the length of the array cannot be specified when defining the array. Moreover, since the definition of the array is only a reference variable and does not point to any valid memory space, there is no memory space to store the array elements, so this array cannot be used, only in numbers The group can only be used after initialization.

2. Creation of one-dimensional array

Create an array object with the keyword new in Java. The format is: array name = new type of array element [number of array elements]

Use new to create an array object, but the default value will be automatically assigned to the array when allocating the array, as follows:

The output is as follows:

Note: once the new keyword is used to allocate memory space for the array, the content stored in each memory space is the value of the array element, that is, the array element has an initial value. Even if the content stored in the memory space is empty, the empty value is null. That is, it is impossible to allocate only the content space without assigning an initial value. Even if you do not specify an initial value when creating an array object (allocating the content space), the system will automatically allocate it

Attachment: the default initialization values of wrapper classes such as basic data types are null, because the array created by wrapper classes of basic data types belongs to reference array (object array), and the default initialization values of object arrays are null

3. Initialization of one-dimensional array

Array initialization is divided into static initialization, dynamic initialization and default initialization:

Static initialization is that the programmer explicitly specifies the initial value of each array element during array initialization, and the array length is determined by the system.

Dynamic initialization means that only the array length is specified during array initialization, and the system allocates the initial value for the array elements.

a. Syntax format of array static initialization:

b. Syntax format of array dynamic initialization:

Attachment: the number of elements cannot be specified during static initialization, but must be specified during dynamic initialization. During static initialization, the array can know the number of elements, so it does not need to be specified. During dynamic initialization, the number of array elements is unknown, so it must be specified.

Note: for one-dimensional array, remember two points. When declaring an array, the size cannot be specified, that is, the square brackets to the left of the equal sign cannot contain numbers. In addition, once the new keyword is used, space must be allocated for the array in memory, and the array must have a default value. Arrays are object data types

Note: do not use static initialization and dynamic initialization at the same time, that is, do not specify the array length and allocate the initial value for each array element during array initialization.

4. Rules for the system to allocate initial values during dynamic initialization of arrays

If the array element type is an integer type (byte, short, int, long) in the basic type, the value of the array element is 0

If the array element type is the floating point type (float, double) in the basic type, the value of the array element is 0.0

If the array element type is the character type (char) in the basic type, the value of the array element is' \ u0000 '

If the array element type is a boolean type in the basic type, the value of the array element is false

If the array element type is a reference type (class, interface, array) in the basic type, the value of the array element is null

Attachment: source code of this part:

The above is the summary of several ways of creating arrays in Java brought by Xiaobian. I hope it can help you and support programming tips~

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>