public class Text {
public static void main(String[] args) {
int[][] a = new int[][]{{1,2},{3,4,5,6}};
}
}
方法2:构造多个一维数组再将多个一维数组赋给二维数组
public class Text {
public static void main(String[] args) {
int[] b1 = new int[]{11,22};
int[] b2 = new int[]{33,44,55,66};
int[][] b = new int[][]{b1,b2};
}
}
不等长二维数组的输出
方法1:根据二维数组中每一行的元素个数多次使用循环
public class Text {
public static void main(String[] args) {
int[][] a = new int[][]{{1,2},{3,4,5,6}};
for(int i = 0; i < 2; i++)
{
System.out.print(a[0][i] + " ");
}
for(int i = 0; i < 4; i++)
{
System.out.print(a[1][i] + " ");
}
}
}
方法2:运用foreach语句
public class Text {
public static void main(String[] args) {
int[] b1 = new int[]{11,22};
int[] b2 = new int[]{33,44,55,66};
int[][] b = new int[][]{b1,b2};
for(int[] i: b)
{
for(int j: i)
{
System.out.println(j);
}
}
}
}