1 PackageCom.baidu.java;2 //initialization of 2-D arrays3 Public classArray2 {4 Public Static voidMain (string[] args) {5 //1. Static initialization6 int[] Aa=New int[][]{{3,4,5},{6,7,8,9},{1,2}};7 //2.1 Dynamic Initialization8String[][] Bb=NewSTRING[6][5];//think of it as 6 groups, 5 people per group9 //2.2 Dynamic InitializationTenString[][] Cc=NewString[6][];//define 6 groups, each with an indeterminate number of members Onecc[0]=NewSTRING[5];//First Group of 5 persons Acc[1]=NewSTRING[6];//second group of 6 persons -cc[2]=NewSTRING[2];//Third Group of 2 persons -cc[3]=NewSTRING[3];//Fourth Group of 3 persons thecc[4]=NewSTRING[6];//Fifth Group of 6 persons -cc[5]=NewSTRING[8];//Sixth Group of 8 persons - //How to assign a value to an element in a two-dimensional array - int[] myint=New int[3] [2]; +myint[0][0]=90;//First number of first row -myint[2][1]=100;//The last number in the third row + //3. Length of two-D array lengths Property ASystem.out.println (myint.length);//3, Number of rows atSystem.out.println (myint[1].length);//2, the length of the second line -System.out.println (cc[5].length);//8, the last of the President - //4. Traversal of two-dimensional arrays -System.out.println ("Iterate through all the elements in the AA array is:"); - for(inti=0;i<aa.length;i++) {//number of rows traversing the AA array - for(intj=0;j<aa[i].length;j++) {//traverse the number of rows per row of AA arrays inSystem.out.print (aa[i][j]+ "");//Output This element - } toSystem.out.println ();//line Break + } -System.out.println ("Iterates through all the elements in the Myint array is:"); the for(inti=0;i<myint.length;i++) {//the number of rows traversing the myint array * for(intj=0;j<myint[i].length;j++) {//iterate through the myint array per row count $System.out.print (myint[i][j]+ "");//Output This elementPanax Notoginseng } -System.out.println ();//line Break the } + } A}
Operation Result:
Initialization of two-dimensional arrays, traversing