String[][] 的越界异常错误
Out of bounds exception error with String[][]
尝试打印二维字符串数组时,我收到一条错误消息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at MainClass.main(MainClass.java:51)
这是我的数组:
String[][] list = {
{"1",null},
{"2",null},
{"3",null},
{"4",null},
{"5",null},
{"6",null},
{"7",null},
{"8",null},
{"9",null},
{"10",null},
{"11",null},
{"12",null},
{"13",null},
{"14",null},
{"15",null}
};
这就是我打印出来的方式:
for( int row=0; row<list.length; row++) {
for( int col=0; col<list.length; col++) {
System.out.print(list[row][col] + "\t"); //this is MainClass:java:51 where the error is happenin
}
System.out.println();
}
我正在尝试以漂亮的网格形状打印出来。我现在这样做的方式确实适用于 Integrarray,所以这让我有点困惑。
col<list.length
应该是 col<list[row].length
尝试打印二维字符串数组时,我收到一条错误消息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at MainClass.main(MainClass.java:51)
这是我的数组:
String[][] list = {
{"1",null},
{"2",null},
{"3",null},
{"4",null},
{"5",null},
{"6",null},
{"7",null},
{"8",null},
{"9",null},
{"10",null},
{"11",null},
{"12",null},
{"13",null},
{"14",null},
{"15",null}
};
这就是我打印出来的方式:
for( int row=0; row<list.length; row++) {
for( int col=0; col<list.length; col++) {
System.out.print(list[row][col] + "\t"); //this is MainClass:java:51 where the error is happenin
}
System.out.println();
}
我正在尝试以漂亮的网格形状打印出来。我现在这样做的方式确实适用于 Integrarray,所以这让我有点困惑。
col<list.length
应该是 col<list[row].length