1. Declaration method of one-dimensional array:
type[] arrayName; or type arrayName[];
Appendix: The first format is recommended because the first format has better readability, indicating that type[] is a reference type (array) rather than a type type. It is recommended not to use the second method
Here is a typical way to declare arrays:
// Declare the integer array int[] intArray0 ; int intArray1 []; // Declare the floating point array float floatArray0 []; float[] floatArray1 ; // Declare the boolean array boolArray0 []; boolean[] boolArray1 ; // Declare the character array char charArray0 []; char[] charArray1 ; // Declare the string array String stringArray0[]; String[] stringArray1; // Declare the array of strings String stringArray0[]; String[] stringArray1; // Int Int intErrorArray1[5];
Note: When declaring an array in Java, it cannot be specified (the number of elements in the array). This is because an 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 set). This reference variable has not yet pointed to any valid memory, so the length of the array cannot be specified when defining an array. Moreover, since defining an array is just a reference variable and does not point to any valid memory space, there is no memory space to store array elements, so this array cannot be used, and can only be used after the array is initialized.
2. Creation of one-dimensional arrays
In Java, use the keyword new to create an array object, the format is: array name = new Type of array element [number of array elements]
// Create an array. If the array is not initialized at the same time, the size must be specified intArray0 = new int[3];// Incorrect way to create an array. If the size is not specified when creating an array, the size must be initialized // intArray1 = new int[];// When creating an array, if the array is not specified, the size of the array must be initialized at the same time. intArray1 = new int[]{0,1,2}; Use new to create an array object, but when allocating an array, the default value will be automatically assigned to the array, as follows:
System.out.println( "intArray0[0]=" + intArray0 [0]); floatArray0 = new float[3]; System. out.println("floatArray0[0]=" + floatArray0[0]); boolArray0 = new boolean[3]; System. out.println("boolArray0[0]=" + boolArray0[0]); charArray0 = new char[3]; System. out.println("charArray0[0]=" + charArray0[0]); stringArray0 = new String[3]; System. out.println("stringArray0[0]=" + stringArray0[0]); The output is as follows:
intArray0[0]=0 floatArray0[0]=0.0 boolArray0[0]=false charArray0[0]= stringArray0[0]=null
Attachment: Once the memory space is allocated to the array using the new keyword, 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 this memory space is empty, this empty is also a value null. In other words, it is impossible to allocate only content space without assigning initial values. Even if you do not specify the initial value when creating an array object (allocating content space), the system will automatically allocate it to it.
Attachment: For example, the default initialization values of the wrapper class of the basic data type are null, because the array created by the wrapper class of the basic data type belongs to the reference array (object array), and the default initialization values of the object array are null.
3. Initialization of one-dimensional arrays
The initialization of an array is divided into static initialization, dynamic initialization and default initialization:
Static initialization is when the array is initialized by the programmer to explicitly specify the initial value of each array element, and the array length is determined by the system.
Dynamic initialization is that when an array is initialized, only the array length is specified, and the system assigns the initial value to the array elements.
a. Syntax format for static array initialization:
arrayName = new type[]{element1,element2,element3...} or use a simplified syntax format: arrayName = {element1,element2,element3...} b. Syntax format for dynamic array initialization:
arrayName = new type[length];
Attachment: The number of elements cannot be specified during static initialization, but the number of elements must be specified during dynamic initialization. The array can know the number of elements during static initialization, so it does not need to be specified, while the number of elements in the array is unknown, so it must be specified.
// Static initialization int intArray2 [] = new int[]{20,21,22};// Static initialization simplified method int intArray3 [] = {30,31,32};// Dynamic initialization int[] intArray4 = new int[3];// Error writing: Static initialization cannot specify the number of elements // int intErrorArray5[] = new int[3]{50,51,52};// Error writing: Dynamic initialization must specify the number of elements // int intErrorArray6[] = new int[];Note: Remember two points in a one-dimensional array. The size cannot be specified when declaring the array, which means that the brackets on the left side 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. Array is an object data type
Note: Do not use static initialization and dynamic initialization at the same time, that is, do not use array initialization again, specify the array length and assign initial values to each array element.
4. Rules for the system to assign initial values when arrays are dynamically initialized
The array element type is an integer type (byte, short, int, long) in the basic type, so the value of the array element is 0.
The array element type is a floating point type (float, double) in the basic type, so the value of the array element is 0.0
The array element type is a character type (char) in the primitive type, so the value of the array element is '/u0000'
The array element type is a boolean in the basic type, so the value of the array element is false
The array element type is a reference type (class, interface, array) in the basic type, so the value of the array element is null
Attachment: This part of the source code:
package javabase;public class CreateArray { public static void main(String args[]){ /************************************/ // Declare integer array int[] intArray0 ; intArray1 []; // Declare floating point array float floatArray0 []; float[] floatArray1 ; // Declare boolean boolArray0 []; boolean[] boolArray1 ; // Declare character array char charArray0 []; char[] charArray1 ; // Declare the string array String stringArray0[];String[] stringArray1; // Incorrect way to declare the array, the size cannot be specified when declaring the array // int [5] intErrorArray0; // intErrorArray1[5]; /********************************* Creation of array *******************************/ // Create an array, if the array is not initialized at the same time, the size must be specified intArray0 = new int[3]; // Incorrect way to create the array, if the size is not specified when creating the array, the size must be initialized // intArray1 = new int[]; // When creating an array, if the array size is not specified, the array must be initialized at the same time as it is created intArray1 = new int[]{0,1,2};System. out.println("intArray0[0]=" + intArray0[0]); floatArray0 = new float[3];System. out.println("floatArray0[0]=" + floatArray0[0]); boolArray0 = new boolean[3];System. out.println("boolArray0[0]=" + boolArray0[0]); charArray0 = new char[3];System. out.println("charArray0[0]=" + charArray0[0]); stringArray0 = new String[3];System. out.println("stringArray0[0]=" + stringArray0[0]); /****************************** Initialization of array****************************/ // Static initialization int intArray2 [] = new int[]{20,21,22}; // Static initialization simplified method int intArray3 [] = {30,31,32}; // Dynamic initialization int[] intArray4 = new int[3]; // Error writing: Static initialization cannot specify the number of elements // int intErrorArray5[] = new int[3]{50,51,52}; // Error writing: Dynamic initialization must specify the number of elements // int intErrorArray6[] = new int[];System. out.println("intArray2[0]=" +intArray2 [0]);System. out.println("intArray3[0]=" +intArray3 [0]);System. out.println("intArray4[0]=" +intArray4 [0]); }}The above is the summary of several ways to create arrays in Java brought to you. I hope it will be helpful to you and support Wulin.com more~