1 Public classarr1 {2 3 4 Public Static voidMain (string[] args) {5 //creates a two-dimensional array arr[][], outputting the and of all elements of the two-dimensional array. 6 7 intarr[][]={{1,3,5,7,9},{21,23,25,27,29},8{12,14,16,18},{32,34,36,38}};9 intSum=0;TenSystem.out.println ("Two-dimensional array traversal"); One //For loop Traversal summation A for(inti=0;i<arr.length;i++){ - for(intj=0;j<arr[i].length;j++){ -sum+=Arr[i][j]; theSystem.out.print (arr[i][j]+ "\ T"); - }system.out.println (); -}system.out.println ("The and = of all elements of the two-dimensional array" +sum); - //foreach Traversal summation + /*for (int [] n:arr) { - for (int ar:n) { + System.out.print (ar+ "\ t"); A Sum+=ar; at } - System.out.println (); - }system.out.println ("The and =" of all elements of the two-dimensional array +sum);*/ - } -}
Second, transpose operation
1 Public classARR2 {2 3 4 Public Static voidMain (string[] args) {5 /*perform a transpose operation on a matrix6 The top left to bottom right slash is the axis7 The number on the axis does not change to the lower left corner to the upper right corner8 In the same place, the number of the upper right corner goes to the lower left corner symmetric rotation transpose operation completed*/9 Ten //The first step is to create a two-dimensional array One intarr1[][]=New int[][]{{2,1,4,5},{6,4,2,1}, A{9,8,7,6},{4,6,8,1}}; -System.out.println ("The matrix before the transpose is:"); -PrintArray (ARR1);//one sentence output matrix two-dimensional array the - - //The second step is to set up a second two-dimensional array space to set the last two-dimensional array length into - intarr2[][]=New int[arr1.length][arr1.length]; + - //step three for loop completion index + for(inti=0;i<arr1.length;i++){ A for(intj=0;j<arr1[i].length;j++){ at //two-dimensional array arr1 assignment to an empty two-dimensional array arr2?? -arr2[j][i]=Arr1[i][j]; - } - } -System.out.println ("The matrix after the transfer is:"); -PrintArray (ARR2);//one sentence output matrix two-dimensional array in } - Private Static voidPrintArray (int[] arr1) to //??? How it came to be, why did it happen? + { - //The fourth step is to establish a two-dimensional array arr2 The index traversal output assignment. the for(inti=0;i<arr1.length;i++){ * for(intj=0;j<arr1[i].length;j++){ $System.out.print (arr1[i][j]+ "");Panax Notoginseng }system.out.println (); - } the } + A}
Three, nine Gongge
1 Public classninetable {2 3 /*4 * Output Nine lattice5 */6 Public Static voidMain (string[] args) {7 // 8 intarr[][]=New int[3] [3];9 intA =2;Ten intb =3/2; One //why the 3/2? A for(inti=1;i<=9;i++){ -arr[a++][b++]=i; - if(i%3==0){ theA=a-2; -B=b-1;// Why? - } - Else{ +a=a%3; -b=b%3; + } A}system.out.println ("Output IX Gongge:"); at for(inti=0;i<3;i++){ - for(intj=0;j<3;j++){ -System.out.print (arr[i][j]+ "\ T"); -}system.out.print ("\ n"); - } - } in -}
2016/1/10 Job 1, two-dimensional array traversal output summation 2, transpose operation???? 3, nine Gongge?? The latter two exist problems