对于一个实践问题,为我的编程类,我们有:错误输出二维String数组转换成一维字符串数组
“定义返回字符串的二维数组具有的第一行的方法字符串名称为“John”。“
public class TwoDimensionalStringArrayI {
public static void main(String[] args) {
String[][] b = {{"John", "Abby"},
{"Sally", "Tom"}};
System.out.println(firstRow(b)); // line 8
}
public static String[] firstRow(String[][] a) {
String[] name = new String[a[0].length];
int counter = 0;
for (int row = 0; row < a.length; row++) {
for (int col = 0; col < a[row].length; col++) {
name[counter++] = a[row][col]; // line 17
}
}
return name;
}
}
通过Eclipse的调试过程后,我的String数组name
设置为{"John", "Abby"}
,但我在8号线和17尝试运行程序时得到一个ArrayIndexOutOfBoundsException
错误。
困惑于如何让程序输出名称“John”和“Abby”。
我试过了,但它只是将名称数组更改为“John”和“Sally”。此外,我仍然得到错误。 – derpt34