Two-dimensional array: is the elements of a one-dimensional array, a two-dimensional array is an array of array elements to understand a two-dimensional array, first understand what a one-dimensional array is. A one-dimensional array is a container that stores the same data type (I won’t go into one-bit arrays here). A two-dimensional array is an array used to store one-dimensional arrays, whose storage data types are basic data types and reference data types, and whose storage data types are reference data types (one-dimensional arrays are reference data types). That is: a two-dimensional array is an array that stores a one-dimensional array, the elements of a two-dimensional array are all arrays, and a two-dimensional array stores a one-dimensional array.
packageJava basic 05_ 2d array;/* * format: * Data type [][] array name =new data type [m][n]; * Extension: Data type [] array name [] = new data type [m][n]; * m: indicates the number of one-dimensional arrays in this two-dimensional array * n: indicates the number of elements in the array */
public class Array2Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[][] strs=new String[3] [4];
/* * STRS [0]: represents the first one-dimensional array, in other words, the first group * STRS [0][1]: represents the second element of the first one-dimensional array */
strs[0] [0] ="Kris Wu";
strs[1] [1] ="Lay xing";
strs[2] [2] ="Huang Zitao";
for(int i=0; i<strs[0].length; i++) { System.out.print(strs[0][i]+",");// Null,null,null,
}
System.out.println();
[] array name =new data type [m][]; * /
String[][] strs2=new String[3] [];// Dynamic memory tool for each one-dimensional array
strs2[0] =new String[2];// Allocate two memory Spaces for one-dimensional data in a two-dimensional array
strs2[1] =new String[4];
strs2[2] =new String[1];
System.out.print(strs2[0].length);/ / 2
System.out.println();
strs2[0] [0] ="Lu Han";
strs2[0] [1] ="Guan Xiaotong";
for(int i=0; i<strs2[0].length; i++) { System.out.print(strs2[0][i]);// Lu Han, Guan Xiaotong
}
System.out.println();
Basic format: / format 3: * * * * data type [] [] [] [] array name = new data type {{elements 1, 2,... },{element 1, element 2,....... }}; * Data type [][] Array name ={{element 1, element 2... },{element 1, element 2,....... }}; * * /
String[][] strs3= {{"Zhang"."Bill"}, {"Fifty"."Daisy"}, {"Money seven"."Sun."}};
String str=strs3[1] [0];// use index value
System.out.println(str);/ / Cathy
System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
System.out.println("Traversal of a two-dimensional array");
for(int i=0; i<strs3.length; i++) { System.out.print("I:"+i);
for(int j=0; j<strs3[i].length; j++) { System.out.print(strs3[i][j]);// Basic data: I is: 0 zhang SAN li Si I is: 1 Wang Wu Zhao Liu I is: 2 Qian Qi Sun Ba} } System.out.println(); }}Copy the code