我想在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