When each element in the array has only one subscript, the array is a one-dimensional array . One-dimensional array is the simplest type of array.
There are two ways to initialize arrays: dynamic initialization and static initialization.
Static initialization means assigning a value to the array when declaring it and calling it directly later.
The static initialization method is as follows:
Array type [] name = new type {element};
For example:
int[]Array=newint[]{1,2,3};
You can also declare it first and then initialize it:
int[]Array;Array=newint[]{1,2,3};
Dynamic initialization means to declare it first and then assign values one by one in subsequent statements.
The method of dynamic initialization is as follows:
int[]Array=newint[array length];Array[0]=1;Array[1]=2;Array[2]=3;Array[3]=4;…Array[array length-1]=n ;
The length of the initialized array is determined.
Static initialization int [] Array = new int[]{1,2,3}; There are 3 elements in the curly braces, so the length of Array is 3.
Dynamic initialization int [] Array = new int [array length]; if [array length] is 3, Array has 3 elements, and its array length is 3.
We can use subscripts (subscripts) to call arrays.
int[]Array;Array=newint[]{1,2,3};System.out.println(Array[1]);
At this time, the output is the second element in the Array, which is output 2.
At this time, some students will ask: Why is it calling the second element even though it is Array[1]? This is because the subscripts of arrays start from 0, that is, Array[0] corresponds to the first element in the array, Array[1] corresponds to the second element in the array, and so on.
The number in the second bracket in dynamic initialization is the length of the array, for example:
int[]Array=newint[3];
This means that the array length of Array is 3. If you want to call the third one, the subscript must be 2:
System.out.println(int[2]);
Note : The called array elements cannot exceed the array length.
For example:
int[]Array=newint[3];System.out.println(int[3]);
At this time, it means the fourth element of the output Array array, but the array length of Array is 3, that is, there are only 3 elements, so an error will be reported.
We can use loop statements to traverse the array, for example:
for(inti=0;i<Array.length;i++){System.out.println(Array[i]);}
It should be noted that i starts from 0 because the array subscript starts from 0. The subscript of the first element of the array is 0. The first element must be included, so the loop starts from 0.
The .length method is the length of the array. i<Array.length means that the value of i is less than the array length of Array. Loop under this condition.
For example:
importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){int[]arr1=newint[]{1,3,5,7,9,11};//Static initialization System.out.println(arr1[ 1]);//Output the second element of arr: 3int[]arr2=newint[5];//Dynamic initialization Scannerscan=newScanner(System.in);System.out.println(please enter:);for( inti=0;i<arr2.length;i++){//Assign arr2 one by one arr2[i]=scan.nextInt();System.out.println(arr2[i]);//Output arr2 one by one}} }
You can try it out yourself!