public class Matrix {
public static void main(String[] args) {
int i,j,m,d;
int matrix[][] = new int[4][4];
i = 1;
j = 1;
m = 1;
d = 1;
while(m <= 4*4){
matrix[i-1][j-1] = m;
switch(d){
case 1: j = j + 1;
if(i == 1)
d = 2;
else
d = 4;
break;
case 2: i = i + 1;
j = j - 1;
if(j == 1)
d = 3;
else if(i == 4)
d = 1;
break;
case 3: i = i + 1;
if(j == 4 )
d = 2;
else
d = 4;
break;
case 4: i = i - 1;
j = j + 1;
if(i == 1)
d = 1;
else if(j == 4)
d = 3;
break;
}
m++;
}
for(int a = 0;a<=4;a++)
for(int b = 0;b<=4;b++)
System.out.println(matrix[a][b]);
}
}
运行出现异常:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
显示这行 matrix[i-1][j-1] = m;