我想在Java中创建一个矩阵。我执行下面的代码使用Java创建一个矩阵
public class Tester {
public static void main(String[] args) {
int[][] a = new int[2][0];
a[0][0] = 3;
a[1][0] = 5;
a[2][0] = 6;
int max = 1;
for (int x = 0; x < a.length; x++) {
for (int b = 0; b < a[x].length; b++) {
if (a[x][b] > max) {
max = a[x][b];
System.out.println(max);
}
System.out.println(a[x][b]);
}
}
System.out.println(a[x][b]);
}
}
当我运行代码,我得到了以下错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at shapes.Tester.main(Tester.java:8)
我试着不同的方法来纠正代码,但没有什么是有帮助的 你能为我纠正代码吗?
谢谢
它没有真正意义上创建一个二维矩阵,其中第二维包含没有维度。决定:一个1维数组,或者每个维度的尺寸至少为1的2维, – lazary
顺便说一下,你的b变量在'for'循环的外部是不可见的 – MGoksu