How to define a two-dimensional array
Data type "" "Name = new data Type" a "" B "
A: There are several one-dimensional arrays represented in this two-dimensional array
B: How many elements are represented in this one-dimensional array, indexed
Data type "" "" name = {{},{},{}};
Traversing a two-dimensional array
Class Lianxi {public static void main (string[] args) {int[][] attr ={{1,2},{3,4},{5,6}};for (int i =0;i<attr.length; i+ +) {for (int j=0;j<attr[i].length; j + +) {System.out.println (attr[i][j]);}}}
Examples:
For example, you want to count the total sales of each group in a company's three sales groups and the sales for the entire company. As shown below
The first group of sales is {11, 12} million
The second group of sales is {21, 22, 23} million
The third group of sales is {31, 32, 33, 34} million.
classLianxi { Public Static voidMain (string[] args) {intA = 0; Define 2 countersintb = 0; int[] attr ={{11,12},{21,22,23},{31,32,33,34}}; for(intI =0;i<attr.length;i++)//Outer loop control each group { for(intJ=0;j<attr[i].length; j + +)//Inner loop controls the addition of elements in a one-dimensional array to get the sales of each group {a= attr[i][j]+A; } System.out.println ("+i+" group sales Total "+"a); Sales for each group B= A +b; Assign a value to B, then clear A to zero, B is the annual sales a= 0; }system.out.println ("Company Annual Sales" +b); }}
Java two-dimensional array