1. Declaration and initialization of one-dimensional arrays, divided into dynamic and static:
View plaincopy to clipboardprint?
// Create 10 integers dynamically. The default value is 0.
Int [] a1 = new int [10];
// Create four integers statically and initialize them as values in parentheses
Int [] a2 = {1, 2, 3, 4 };
// Dynamically create a MyClass array. You need to traverse each class instance in the created array.
MyClass [] a3 = new MyClass [3];
For (int I = 0; I <a3.length; ++ I)
MyClass [I] = new MyClass ();
// Create a static MyClass array and use the instance in brackets to initialize the Array
MyClass [] a4 = {new MyClass (), new MyClass (), new MyClass };
// Create 10 integers dynamically. The default value is 0.
Int [] a1 = new int [10];
// Create four integers statically and initialize them as values in parentheses
Int [] a2 = {1, 2, 3, 4 };
// Dynamically create a MyClass array. You need to traverse each class instance in the created array.
MyClass [] a3 = new MyClass [3];
For (int I = 0; I <a3.length; ++ I)
MyClass [I] = new MyClass ();
// Create a static MyClass array and use the instance in brackets to initialize the Array
MyClass [] a4 = {new MyClass (), new MyClass (), new MyClass };
2. Multi-dimensional arrays can be understood as arrays. They can also be declared in two forms:
View plaincopy to clipboardprint?
Public class Main {
Static void printInt (int [] [] ints ){
For (int I = 0; I <ints. length; ++ I)
For (int j = 0; j <ints [I]. length; ++ j)
System. out. println (ints [I] [j]);
}
Public static void main (String [] args ){
// Dynamically create a two-dimensional array and traverse the initialization
Int [] [] ints = new int [2] [2];
For (int I = 0; I <ints. length; ++ I)
For (int j = 0; j <ints [I]. length; ++ j)
Ints [I] [j] = I + j;
PrintInt (ints );
// Create a two-dimensional array statically
Int [] [] ints2 = {1, 2}, {3, 4 }};
PrintInt (ints2 );
}
}
Public class Main {
Static void printInt (int [] [] ints ){
For (int I = 0; I <ints. length; ++ I)
For (int j = 0; j <ints [I]. length; ++ j)
System. out. println (ints [I] [j]);
}
Public static void main (String [] args ){
// Dynamically create a two-dimensional array and traverse the initialization
Int [] [] ints = new int [2] [2];
For (int I = 0; I <ints. length; ++ I)
For (int j = 0; j <ints [I]. length; ++ j)
Ints [I] [j] = I + j;
PrintInt (ints );
// Create a two-dimensional array statically
Int [] [] ints2 = {1, 2}, {3, 4 }};
PrintInt (ints2 );
}
}
3. variable array declaration form:
View plaincopy to clipboardprint?
Public class Main {
Static void printInt (int [] [] ints ){
For (int I = 0; I <ints. length; ++ I)
{
For (int j = 0; j <ints [I]. length; ++ j)
System. out. print (ints [I] [j] + "");
System. out. println ();
}
}
Public static void main (String [] args ){
// Dynamically create a two-dimensional variable array, first create the first dimension
Int [] [] ints = new int [2] [];
// Determine the number of elements in the second dimension.
For (int I = 0; I <ints. length; ++ I)
Ints [I] = new int [I + 3];
PrintInt (ints );
// Create a two-dimensional variable array statically
Int [] [] ints2 = {1, 2}, {3, 4, 5 }};
PrintInt (ints2 );
}
}
// The output is:
0 0 0
0 0 0 0
1 2
3 4 5