我最近刚开始使用 java,在学习了一些语法后,我决定 写一个国际象棋游戏来教自己更多。
我试图创建一个字符串数组的数组来存储板的基本 View ,但是当我打印出来时,一切都是空的。
private String board[][] = new String[8][8];
public Board() {
System.out.println("created");
for (String[] row : board) {
for (String cell : row) {
cell = "-";
}
}
printBoard();
}
感觉我有点搞乱了 foreach 或 string 的概念。
提前致谢,
或者
最佳答案
cell = "-";
此赋值发生在 for 循环中声明的局部变量上。
您需要访问数组中的每个元素并分配字符串。
for (int i=0; i<board.length; i++) {
for (int j=0; j<board[i].length; j++) {
board[i][j] = "-";
}
}
这里board.length
是String[]
(行)和board[i].length
的数量以及数字每个数组(列)中的字符串
。
关于java - java中二维字符串数组的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34358868/