In Java, a two-dimensional array is regarded as an array of arrays , that is, a two-dimensional array is a special one-dimensional array, and each element of the two-dimensional array is a one-dimensional array.
Data type of element [][] Name of array = new Data type of element [length of two-dimensional array][length of one-dimensional array];
int[][]arr=newint[3][2];
The above situation represents that there are three one-dimensional arrays, and each one-dimensional array has 2 elements.
Data type of element [][] Name of array = new Data type of element [length of two-dimensional array][];
int[][]arr=newint[3][];
The above situation represents that there are three one-dimensional arrays, but the number of elements in each one-dimensional array is uncertain, and the number of elements in each one-dimensional array can be different.
Data type of element [][] Array name = {{Element 1, Element 2,…}, {Element 1, Element 2,…},…};
int[][]arr={{1,2,3},{4,5},{6,7,8,9}};
The above situation represents that there are 3 one-dimensional arrays. The first one-dimensional array has 3 elements, the second one-dimensional array has 2 elements, and the third one-dimensional array has 4 elements. Each The number of elements in a one-dimensional array can be different.
There are two formats for declaring a two-dimensional array:
Array element type array name[][];
Element type of array[][] array name;
For example:
charcat[][];char[][]cat;
The initialization of two-dimensional arrays is the same as that of same-dimensional arrays. There are two ways: dynamic initialization and static initialization.
1) Declare first and then initialize statically
Data type of element [][] two-dimensional array name; two-dimensional array name = new data type of element [][]{{element 1, element 2, element 3,...},{value list in the second row}, …,{List of values in row n}};
2) Static initialization while declaring
Data type of element [][] Two-dimensional array name = new Data type of element [][]{{Element 1, Element 2, Element 3,…},{Value list of the second row},…,{nth list of row values }};
Note : Numbers cannot be written in "[][]" in the "data type of new element [][]" on the right, because the number of rows and columns is determined by the number of elements in {}.
3) Simplified writing method of static initialization at the same time of declaration
Data type of element [][] Two-dimensional array name = {{element 1, element 2, element 3,…},{value list in the second row},…,{value list in the nth row}};
For example:
publicclassMain{publicstaticvoidmain(String[]args){int[][]arr={{1,2,3},{4,5},{6}};//Define array System.out.println(arr[0 ][0]);//1System.out.println(arr[ 1][0]);//4System.out.println(arr[2][0]);//6System.out.println(arr[0][1]);//2System.out.println(arr [1][1]);//5System.out.println(arr[2][1]);//Out of bounds}}
Dynamic initialization creation does not need to declare the length of the one-dimensional array inside. Therefore, the length of each one-dimensional array can be different. Therefore, there are the following two initialization methods.
1) Dynamic initialization 1: Rule two-dimensional table
The programmer specifies the length of the array and assigns the value later. At the beginning, the system will give the default initial value of the element. A regular two-dimensional table means that the number of columns in each row is the same. Its general format is as follows. First determine the number of rows and Number of columns:
Data type of element [][] Two-dimensional array name = data type of new element [m][n];
Where m represents how many one-dimensional arrays there are in this two-dimensional array, or how many rows there are in the two-dimensional table; n represents how many elements there are in each one-dimensional array, or how many cells there are in each row.
At this point, the array is created, the number of rows and columns is determined, and the elements have default values. Then assign new values to the elements:
Two-dimensional array name[row subscript][column subscript]=value;
For example:
publicclassMain{publicstaticvoidmain(String[]args){int[][]arr=newint[3][2];/*Define a two-dimensional array arr. This two-dimensional array has three one-dimensional array elements. Each one-dimensional array There are 2 elements */Syst em.out.println(arr);//Output the name of the two-dimensional array System.out.println(arr[0]);//Output the name of the first element of the two-dimensional array and the name of the one-dimensional array System.out.println( arr[0][0]);//Output the elements of the two-dimensional array}}
2) Dynamic initialization 2: irregular two-dimensional table
An irregular two-dimensional table means that the number of columns in each row may be different. Its general format is as follows. First determine the total number of rows:
Data type of element [][] Two-dimensional array name = data type of new element [total number of rows][];
At this time, only the total number of rows is determined, and each row is now null. Then the number of columns in each row is determined, and a one-dimensional array of each row is created:
Two-dimensional array name [row subscript] = data type of new element [total number of columns in the row];
At this time, the elements of the rows that pass new have default values, and the rows that do not pass new are still null. Finally, the elements are assigned a value:
Two-dimensional array name[row subscript][column subscript]=value;
Note : Irregular two-dimensional tables, after declaration, cannot be abbreviated when initializing the internal one-dimensional array.
//Irregular two-dimensional table defines two-dimensional array int[][]arr=newint[3][]; //3 rows, unknown columns //Initialize the element value in the two-dimensional array arr[0]=newint[ ]{1};//This cannot be abbreviated as arr[0]={1}arr[1]=newint[]{2,3};arr[2]=newint[]{4,5,6};
The traversal of a two-dimensional array is the same as that of a one-dimensional array. The difference is that first, an outer loop is required to traverse each one-dimensional array. The general format is as follows:
for(inti=0;i<two-dimensional array name.length;i++){for(intj=0;j<two-dimensional array name[i].length;j++){System.out.print(two-dimensional array name[ i][j]);}System.out.println();}