2 回答

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊
問(wèn)題是你的循環(huán):
for (int i = 0; to returncharLength; increment i) {
for (int j = 0; to returnchar[i]Length; increment j) {
returnChar[i][j] = spliter[0].charAt(j);
}
}
將覆蓋整個(gè)數(shù)組,因?yàn)槊看味紡?row 轉(zhuǎn)到 , 0。returnchar.length相反,您只需要填充最后一行之后的下一行。您可以for按如下方式重寫(xiě)循環(huán):
for (int r=0; r<returnchar.length; r++) { // "r" for row
String line = input.nextLine();
String[] splitter = line.split(" ", 2);
for (int c=0; c<returnchar[r].length; c++) { // "c" for column
returnchar[r][c] = splitter[c];
}
}

TA貢獻(xiàn)1744條經(jīng)驗(yàn) 獲得超4個(gè)贊
這是打印漂亮的 2D 數(shù)組的方法:
for (char[] x : returnChar) { System.out.println(Arrays.toString(x)); }
添加回答
舉報(bào)