1. How to declare a one-dimensional array:
Type[] Arrayname; or type arrayname[];
Attached: The first format is recommended because the first format is better readable, which means that type[] is a reference type (array) rather than a type. It is recommended that you do not use the second method
The following is a typical way to declare an array:
Declares an integer array
int[] intArray0;
int IntArray1 [];
Declares a floating-point array of
float floatArray0 [];
Float[] FloatArray1;
Declare a Boolean array boolean
boolArray0 [];
Boolean[] BoolArray1;
Declares a character-type array
char charArray0 [];
Char[] CharArray1;
Declares a string array string
stringarray0[];
String[] StringArray1;
The wrong way to declare an array, when declaring an array, cannot specify its size
//int [5] intErrorArray0;
int interrorarray1[5];
Note: You cannot specify its length (the number of elements in an array) when declaring an array in the Java language. This is because an array is a variable of a reference type, so when you use it to define a variable, only a reference variable is defined (that is, a pointer is set), and the reference variable does not point to any valid memory. Therefore, you cannot specify the length of an array when defining an array. And since the definition array is just 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 is not available and can only be used if the array is initialized.
2, one-dimensional array creation
In Java, use the keyword new to create an array object in the form of: array name = new array element type [number of array elements]
Creates an array, and must specify its size
intArray0 = new Int[3] If it is created without initializing the array;
Wrong way to create an array, and you must initialize
//intArray1 = new int[When you create an array without specifying a size;
When creating an array, you must initialize the array
intArray1 = new int[]{0,1,2} When you create it without specifying the size of the array;
Creating an Array object with new but assigning an array automatically assigns the default values to the group, 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 an array using the New keyword, the contents of each memory space are the values of the elements of the element, that is, the array element has an initial value, even if the contents of the memory space are empty, this null is also a value null. That is, it is not possible 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 automatically assigns it
Attached: wrapper classes, such as underlying data types, whose default initialization value is NULL, because the wrapper class for the underlying data type creates an array of reference arrays (object arrays), and the default initialization value for the object array is 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 an array is initialized by the programmer to explicitly specify the initial value of each array element and the length of the array is determined by the system.
Dynamic initialization is an array that specifies only the length of an array when initialized, and the initial value is allocated by the system's array elements.
A, the syntax format for static initialization of arrays:
Arrayname = new Type[]{element1,element2,element3 ...}
or use a simplified syntax format:
arrayname = {Element1,element2,element3 ...}
b, the syntax format for dynamic initialization of the array:
Arrayname = new Type[length];
Attach: The number of elements cannot be specified when statically initialized, and the number of elements must be specified when dynamic initialization occurs. The array can know the number of elements when statically initialized, so it is not required, and the number of array elements is unknown when dynamic initialization must be specified.
Static initialization
int IntArray2 [] = new int[]{20,21,22};
Static initialization simplified way
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 wording: Dynamic initialization must specify the number of elements
//int interrorarray6[] = new int[];
Note: one-dimensional array this piece remembers two points, and the array declaration cannot specify the size, which means that the brackets on the left side of the equals sign cannot contain numbers. In addition, once the new keyword is used then it is definitely allocated space in memory, then the inevitable array has a default value. Arrays are object data types
Note: Do not use static initialization and dynamic initialization at the same time, that is, when array initialization is not done, you specify both the length of the array and the initial value for each array element.
4, the array of dynamic initialization of the system allocation of the initial value of the rules
The array element type is an integer type (byte, short, int, long) in the base type, the value of the array element is 0
The array element type is a floating-point type in the base type (float, double), the value of the array element is 0.0
The array element type is the character type (char) in the base type, the value of the array element is ' \u0000 '
Array element type is a Boolean type in the base type (Boolean), the value of the array element is False
The array element type is the reference type (class, interface, array) in the base type, the value of the array element is NULL
Attached: This part of the source code:
Package javabase; public class Createarray {public static void main (String args[]) {/************** array declaration *******************///Declaration integer number
Group int[] IntArray0;
int IntArray1 [];
Declares a floating-point array of float floatArray0 [];
Float[] FloatArray1;
Declare a Boolean array boolean boolArray0 [];
Boolean[] BoolArray1;
Declares a character-type array char charArray0 [];
Char[] CharArray1;
Declares a string array string stringarray0[];
String[] StringArray1;
The wrong way to declare an array, when declaring an array, cannot specify its size//int [5] intErrorArray0;
int interrorarray1[5];
/********************* the creation of an array ***********************///creates an array, and must specify its size intArray0 = new Int[3 If the array is not initialized at the time it is created;
Wrong way to create an array, and you must initialize//intArray1 = new int[When you create an array without specifying a size;
When creating an array, you must initialize the array intArray1 = new int[]{0,1,2} When you create it without specifying the size of the array; 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 the/********************** array *************************///static initialization int IntArray2 [] = new int[]{20,21,22};
Static initialization simplified way 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 wording: 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 a small series of Java to create an array of several ways to sum up all the content, I hope to help you, a lot of support cloud Habitat Community ~