首先,初学者在这里.
我正在使用此代码.
class MDArrays {
public static void main(String[] args) {
int[][] x;
int t=2;
x = new int[2][3];
for(int i=0; i<=1; i++) {
for(int j=0; i<=2; j++) {
x[i][j] = t;
t += 2;
System.out.println(x[i][j]);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
它编译得很完美,但在运行时,正确显示3个数字后,我收到以下错误.
Exception in thread "main" java.Lang.ArrayindexOutOfBoundsException : 3 at MDArrays.main(MDArrays.java:13)
我哪里错了?
在检查i时你正在递增j.
for(int j=0; i<=2; j++)
Run Code Online (Sandbox Code Playgroud)
j将继续递增,最终会给你一个IndexOutOfBoundsException