Java Study Notes-array
Software Version:
Java version "1.7.0 _ 05"
Eclipse
Directory:
1. Declare an array
2. instantiate an array
3. initialize the Array
4. Two-dimensional array
5. Length attribute
6. foreach Loop
7. References
1. Declare an array
JAVA supports two formats for declaring Arrays:
Type [] arrayname;
Type arrayname []; // write like this without pushing
Note that the declared array cannot be used. It only declares an array whose arrayname is of the type and does not point to any valid memory space (equivalent to a pointer). Therefore, it cannot be used. During the definition process, you cannot specify the length of the array in square brackets.
The length of an array is determined during array instantiation or array initialization. Only the instantiated or initialized array can be used.
2. instantiate an array
The so-called instantiation is actually allocating memory space for arrays in the heap. Format:
1)
Type [] arrayname; // declare an array
Arrayname = new type [length]; // instantiate an array and allocate memory for the Array
2)
Type [] arrayname = new type [length]; // declare and instantiate an array
3. initialize the Array
Initializing an array not only instantiates an array, but also assigns an array initial value. Format:
1)
Type [] arrayname; // declare an array
Arrayname = new type [] {element1, element2, element3,...}; // array of initial calls
2)
Type [] arrayname = {element1, element2, element3,...}; // define and initialize the array... (1)
It should be noted that the initialization below will not work.
Type [] arrayname; // defines (Declaration) Arrays
Arrayname = {element1, element2, element3,...}; // note the difference with (1)
It is not feasible to specify the length and assign values:
1)
Type [] arrayname; // declare an array
Arrayname = new type [length] {element1, element2, element3,...}; // array of initial calls
2)
Type [] arrayname = new type [length] {element1, element2, element3,...}; // define and initialize the Array
4. Two-dimensional array
We can understand two-dimensional arrays in this way, that is, to create an array to store multiple one-dimensional arrays of different lengths. An n-dimensional array can be understood as an array that stores multiple n-1-dimensional arrays of different lengths. Two-dimensional array declaration method:
Type [] [] arrayname;
Type arrayname [] []; // This is not recommended
Type [] arrayname []; // This strange writing method is also supported
Instantiation:
1)
Type [] [] arrayname = new type [length1] [length1]; // declare and instantiate
2)
Type [] [] arrayname = new type [length1] []; // it is acceptable to instantiate only the first-level array, but not just the second-level array.
Arrayname [num1] = new type [lengh2]; // instantiate the second-level Array
Arrayname [num2] = new type [length3]; // their length can be different
Initialization:
Type [] [] arrayname = new type [] [] {elements1...}, {elements2....}, {elements2 ...},...};
Type [] [] arrayname = {elements1...}, {elements2....}, {elements2....},...}; //
This looks a little complicated, and it is easy to understand for an example.
int[][] numbers = new int[][]{{2, 3}, {5, 10}, {20, 30}};String[][] str = new String[][]{new String[3], new String[]{"hello"}};
5. Length attribute
All arrays in Java provide a Length attribute, indicating the length of the array. We can use it to traverse the entire array.
for(int i = 0; i < ArrayName.length; i++){ System.out.println(ArrayName[i]);}
What if arrayname is a two-dimensional array? The length of the first-level array is output.
6. foreach Loop
The foreach loop is used to traverse arrays and collections cyclically. Format:
For (type variablename: arrayname | collection)
{
// Each cycle variablename gets an element in arrayname until the last one jumps out of the loop.
}
Example:
1 public class test { 2 public static void main(String[] args) { 3 4 int[][] numbers = new int[][]{{2, 3}, {20, 30, 40}, {200, 300}, {2000, 3000}}; 5 6 for(int i[] : numbers) 7 { 8 for(int j : i) 9 {10 System.out.println(j);11 }12 }13 }14 }
Since numbers is a two-dimensional array, the I of the 6th rows is a one-dimensional array. In this way, I can point to the first-level array of each nembers.
7. References
[1]. Crazy Java handout Li Gang
Http://android.yaohuiji.com/archives/3379.