Before we introduced a one-dimensional array to store a linear set of elements, we can use a two-dimensional array to store a matrix or table.
1. Declare a two-dimensional array variable and create a two-dimensional array
The following is the syntax for declaring a two-dimensional array: data type [][] array name; for example, int[][] matrix; You can use this syntax to create a 5*5 int type two-dimensional array and assign it to matrix;
int[][] matrix = new int[5][5];
Two subscripts are used in a two-dimensional array, one representing a row and one representing a column. The same dimension array is the same. Each subscript retrieval is int type, starting from 0.
Note: Using matrix[2,1] to access the elements in the second row and first column is a common mistake. In Java, each subscript must be placed in a pair of square brackets.
2. Get the length of the two-dimensional array
A two-dimensional array is actually an array. Each element of it is a one-dimensional array. The length of the array x is the number of elements in the array. You can use x.length to get the value. The two-dimensional array can be regarded as a one-dimensional array. A one-dimensional array, each element of which is another one-dimensional array.
Each row in a two-dimensional array is an array, so the length of each row can be different. Such an array becomes a sawtooth array. If you don't know the value of the sawtooth array in advance, but know its length, you can use the following statement:
int [][] triangleArray = new int[5][];
triangleArray[0] = new int[5];
triangleArray[1] = new int[4];
triangleArray[2] = new int[3];
triangleArray[3] = new int[2];
triangleArray[4] = new int[1];
When creating an array using the syntax new int[5][], you must specify the first subscript. The syntax new int[][] is wrong.
3. Processing two-dimensional arrays
(1) The following loop initializes the array with user input values:
java.util.Scanner input = new Scanner(System.in);
System.out.println("Enter"+matrix.length+"rows and"+matrix[0].length+"columns:");
for(int row = 0;row<matrix.length;row++){
for(int column=0;column<matrix[row].length;column++)
matrix[row][column] = input.nextInt();
}
}
(2) Initialize the array with random values from 0 to 99
for(int row = 0;row<matrix.length;row++){
for(int column=0;column<matrix[row].length;column++)
matrix[row][column] = (int)(Math.random()*100);
(3) Shuffle all the elements in the two-dimensional array, for each element matrix[i][j], randomly generate subscripts i1 and j1, and then exchange the two.
for(int row = 0;row<matrix.length;row++){
for(int column=0;column<matrix[row].length;column++){
int i1 = (int)(Math.random()*matrix.length);
int j1 = (int)(Math.random()*matrix[row].length);
int temp = matrix[i][j];
matrix[i][j] = matrix[i1][j1];
matrix[i1][j1] = temp;
}
}
4. Pass a two-dimensional array to the method
Like passing a one-dimensional array, we pass a two-dimensional array to the method. Let's give an example below to return the sum of all elements in the matrix. The program list is as follows:
package passtwodimensionalaray;
import java.util.Scanner;
/**
*
* @author mjd
*/
public class PassTwoDimensionalAray {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
int[][]m=new int[3][4];
System.out.println("Enter"+m.length+"rows and"+m[0].length+"columns:");
for(int i = 0;i<m.length;i++)
for(int j=0;j<m[i].length;j++)
m[i][j] = input.nextInt();
System.out.println("\nSum of all elements is"+sum(m));
}
public static int sum(int[][]m){
int total =0;
for(int row = 0;row<m.length;row++){
for(int column=0;column<m[row].length;column++){
total=total+m[row][column];
}
}
return total;
}
}
Input a two-dimensional array with three rows and four columns, output the result and the program runs correctly.