Source: http://www.imooc.com/code/1571
The so-called two-dimensional array, which can be simply understood as a "special" one-dimensional array, each of its array space holds a one-dimensional array.
So how to use a two-dimensional array, the steps are as follows:
1. Declaring arrays and allocating space
Or
Such as:
2. Assign Value
The assignment of a two-dimensional array, similar to a one-dimensional array, can be assigned individually by subscript, noting that the index starts at 0
You can also assign a value to an array while declaring it
Such as:
3. Working with arrays
A two-dimensional array accesses and outputs the same array of dimensions, just one more subscript. In the loop output, you need to embed a loop inside, even if you are using a double loop to output each element in a two-dimensional array. such as:
Operation Result:
What you need to know: When you define a two-dimensional array, you can specify only the number of rows, and then specify the number of columns for each row, respectively. If the number of columns per row is different, an irregular two-dimensional array is created, as follows:
The result of the operation is:
Task
Completion time to fill in the blanks!!!
Functional requirements: Define a two-dimensional array of three columns names and assign values, using a binary loop to output the elements in a two-dimensional array.
Operation Result:
Please add the Code Editor 5, 8, and line codes to complete
1 Public classHelloWorld {2 Public Static voidMain (string[] args) {3 4 //define two-dimensional arrays of three columns and assign values5names={{"Tom", "Jack", "Mike"},{"Zhangsan", "Lisi", "Wangwu"}};6 7 //outputs the values of elements in a two-dimensional array through a double loop8 for(inti = 0; I <; i++) {9 Ten for(intj = 0; J < Names[i].length; J + +) { One A System.out.println (); - } - the System.out.println (); - } - } -}
Myans:
1 Public classHelloWorld {2 Public Static voidMain (string[] args) {3 4 //define two-dimensional arrays of three columns and assign values5String[][] names={{"Tom", "Jack", "Mike"},{"Zhangsan", "Lisi", "Wangwu"}};6 7 //outputs the values of elements in a two-dimensional array through a double loop8 for(inti = 0; i < names.length; i++) {9 Ten for(intj = 0; J < Names[i].length; J + +) { One A System.out.println (Names[i][j]); - } - the System.out.println (); - } - } -}
Mu class net-java first season-6-9