Declaration and initialization of Java two-dimensional array
1. A two-dimensional array can be regarded as an array with arrays as elements;
2. The declaration and initialization of two-dimensional arrays in Java should be carried out in order from high dimension to low dimension.
Example:
Static initialization:
Array2.java:
program code
public class Array2 { public static void main(String args[]) { int a[][] = {{1,2},{3,4,5,6},{7,8,9}}; for( int i=0 ; i <a.length ; i++) { for(int j=0 ; j<a[i].length ; j++) { System.out.println("a[" + i + "][" + j + "]=" + a[i][j]) ; } } } }
Dynamic initialization:
program code
public class Array2D { public static void main(String args[]) { int i, j; String s[][]; s = new String[3][]; s[0] = new String[2]; s[ 1] = new String[3] ; s[2] = new String[2] ; for(i=0 ; i<s.length ; i++) { for(j=0 ; j <s[i].length ; j++) { s[i][j] = new String("My position is: " + i + "," + j) ; } } for(i=0 ; i<s.length ; i++) { for(j= 0 ; j<s[i].length ; j++) { System.out.println(s[i][j]) ; } } } }
About two-dimensional array operations in Java [example]:
public class Lesson{ public static void main(String [] args){ //How to declare a two-dimensional array: //Data type [][] Array name = new Data type [length][Length]; //Data type [ ][] Array name = {{123},{456}}; /* int [][] arr = {{123},{456}}; //Define a two-dimensional array with two rows and three columns and assign values for(int x = 0; x<arr.length; x++){ //Locate the row for(int y = 0; y<arr[x].length; y++){ //Locate the number of elements in each line System.out.print(arr[x][y]); } System.out.println("/n"); } */ int [][] num = new int [3][3]; //Define a two-dimensional array with three rows and three columns num[0][0] = 1; //Assign the value to the first element of the first row num[0][1] = 2; //Assign the value num[0][2] = 3 to the second element in the first row; //Assign the value num[1][0] = 4 to the third element in the first row; //Assign the first element in the second row elements are assigned num[1][1] = 5; //Assign the value num[1][2] = 6 to the second element in the second row; //Assign the value num[2][0] = 7 to the third element in the second row; //Assign the first element in the third row Assign num[2][1] = 8 to elements; //Assign num[2][2] = 9 to the second element in the third row; //Assign value to the third element in the third row for(int x = 0; x<num.length; x++){ //Locate row for(int y = 0; y<num[x].length; y++){ //Locate the number of elements in each line System.out.print(num[x][y]); } System.out.println("/n") ; } } } //The array value arr[x][y] indicates that the specified value is the value of row x and column y. //When using a two-dimensional array object, pay attention to the length represented by length. //Add length (such as arr.length) directly after the array name, which refers to the number of rows (Row); //Add after specifying the index. The above length (such as arr[0].length) refers to the elements owned by the row, that is, the number of columns (Column).
Let's look at another example: two two-dimensional arrays, integrate them into a new two-dimensional array, and the elements are the sum of the corresponding elements of the two arrays. Input two arrays: {{1,5},{2,3},{6,5}}, {{4,2},{2,6},{5,7}}
Output printing: {{5,7},{4,9},{11,12}}
Code: class arraysCtrl{ static void arraysAdd(int[][] a,int[][] b) { StringBuffer sbResult = new StringBuffer("{"); int[][] result = new int[a.length][ b.length]; for(int i=0;i<a.length;++i) { sbResult.append("{"); for(int j=0;j<a[i].length;++j) { result[i][j] = a[i][j]+b[i][j]; sbResult.append(result[i] [j]); if(j == a[i].length-1) { if(i == a.length-1) { sbResult.append("}"); }else{ sbResult.append("},"); } break; } sbResult.append(","); } if(i == a.length-1) { sbResult.append("}"); break; } } System .out.println(sbResult); } public static void main(String[] args) { int[][] aTst1 = {{1,5},{2,3},{6,5}}, aTst2 = {{4,2},{2,6},{5,7}}; arraysAdd(aTst1,aTst2); } }