In java, arrays are the simplest type of composite data. An array is a set of ordered data. Each element in an array has the same data type. You can use a unified array name and subscript to uniquely identify the elements in the array. Arrays include one-dimensional arrays and multi-dimensional arrays.
1. Definition of one-dimensional array
Type arrayName [];
Type can be any data type in Java, including simple type and composite type.
For example:
Int intArray [];
Date dateArray [];
2. initialize a one-dimensional array
(1) Static Initialization
Int intArray [] = {1, 2, 3, 4 };
String stringArray [] = {"abc", "How", "you "};
(2) Dynamic Initialization
1) simple array
Int intArray [];
IntArray = new int [5];
2) composite Array
String stringArray [];
String stringArray = new String [3];/* opens a reference space (32-bit) for each element in the array )*/
StringArray [0] = new String ("How"); // open up space for the first array element
StringArray [1] = new String ("are"); // opens space for the second array element
StringArray [2] = new String ("you"); // opens space for the third array element
3. Reference of one-dimensional array elements
The reference method of array elements is as follows:
ArrayName [index]
Index is an array subscript, which can be an integer constant or expression. The subscript starts from 0. Each array has an attribute length to specify its length. For example, intArray. length indicates the length of the array intArray.
Multi-dimensional array
In Java, multi-dimensional arrays are considered as arrays of arrays.
1. Two-dimensional array Definition
Type arrayName [] [];
Type [] [] arrayName;
2. Two-dimensional array Initialization
(1) Static Initialization
Int intArray [] [] = {1, 2}, {2, 3}, {3, 4, 5 }};
In Java, because the two-dimensional array is regarded as an array, the array space is not continuously allocated, so the size of each dimension of the Two-dimensional array is not required to be the same.
(2) Dynamic Initialization
1) directly allocate space for each dimension. The format is as follows:
ArrayName = new type [arrayLength1] [arrayLength1];
Int a [] [] = new int [2] [3];
2) from the highest dimension, allocate space for each dimension:
ArrayName = new type [arrayLength1] [];
ArrayName [0] = new type [arrayLength20];
ArrayName [1] = new type [arrayLength21];
...
ArrayName [arrayLength1-1] = new type [arrayLength2n];
3) For example, the dynamic initialization of a two-dimensional simple data array is as follows,
Int a [] [] = new int [2] [];
A [0] = new int [3];
A [1] = new int [5];
For an array of two-dimensional Composite data types, the reference space must be allocated for the maximum dimension first, and then allocated space for the Low Dimension in sequence. In addition, space must be allocated separately for each array element.
For example:
String s [] [] = new String [2] [];
S [0] = new String [2]; // allocate reference space for the maximum dimension
S [1] = new String [2]; // allocate reference space for the maximum dimension
S [0] [0] = new String ("Good"); // allocate space for each array element
S [0] [1] = new String ("Luck"); // allocate space for each array element
S [1] [0] = new String ("to"); // allocate space for each array element
S [1] [1] = new String ("You"); // allocate space for each array element
3. Reference of two-dimensional array elements
For each element in a two-dimensional array, the reference method is: arrayName [index1] [index2]
For example:
Num [1] [0];
4. Two-dimensional array example:
[Example] multiply two matrices
Public class MatrixMultiply {
Public static void main (String args []) {
Int I, j, k;
Int a [] [] = new int [2] [3]; // dynamically initializes a two-dimensional array.
Int B [] [] = {,}, {, 10,-3}, {,-5,-18}; // static Initialization
A two-dimensional array
Int c [] [] = new int [2] [4]; // dynamically initializes a two-dimensional array
For (I = 0; I <2; I ++)
For (j = 0; j <3; j ++)
A [I] [j] = (I + 1) * (j + 2 );
For (I = 0; I <2; I ++ ){
For (j = 0; j <4; j ++ ){
C [I] [j] = 0;
For (k = 0; k <3; k ++)
C [I] [j] + = a [I] [k] * B [k] [j];
}
}
System. out. println ("******** Matrix C *********"); // print the Matrix C tag
For (I = 0; I <2; I ++ ){
For (j = 0; j <4; j ++)
System. out. println (c [I] [j] + "");
System. out. println ();
}
}
}