Does java have multidimensional arrays?
•
Java
type[][] arrName;
arrName = new type[length][]
public class TwoDimensionTest {
public static void main(String[] args) {
// 定义一个二维数组
int[][] a;
// 把a当成一维数组进行初始化,初始化a是一个长度为4的数组
// a数组的数组元素又是引用类型
a = new int[4][];
// 把a数组当成一维数组,遍历a数组的每个数组元素
for (int i = 0,len = a.length; i < len; i++) {
System.out.println(a[i]); // 输出 null null null null
}
// 初始化a数组的第一个元素
a[0] = new int[2];
// 访问a数组的第一个元素所指数组的第二个元素
a[0][1] = 6;
// a数组的第一个元素是一个一维数组,遍历这个一维数组
for (int i = 0,len = a[0].length; i < len; i++) {
System.out.println(a[0][i]); // 输出 0 6
}
}
}
//Initialize two dimensions of two-dimensional array at the same time int [] [] B = New Int [3] [4];
//Use static initialization syntax to initialize a two-dimensional array string [] [] STR1 = new string [] [] {new string [3], new string [] {"hello"}}// Use the simplified static initialization syntax to initialize the two-dimensional array string [] [] STR2 = {new string [3], new string [] {"hello"}};
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
