当尝试打印二维字符串数组时,我收到一条错误消息:
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
关于java - String[][] 出现越界异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31863792/