尝试打印二维字符串数组时,出现错误消息:
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();
}
我正在尝试以漂亮的网格形状打印出来。我现在的工作方式确实适用于Integr数组,所以这让我感到困惑。
最佳答案
col<list.length
应该是col<list[row].length