在我熟悉面向对象编程之前,我曾创建过一个基本的井字棋游戏,并使用了一个数组来创建棋盘。
由于我没有正确理解如何使用对象,所以代码非常混乱,但我确实正确初始化了棋盘:
char[][] board = new char[3][3];
for (int i = 0; i < board.length; i++){
for (int j = 0; j < board[i].length; j++){
board[i][j] = '[]' //or something like that...don't remember exactly
}
}
我的问题是如何使用ArrayList实现这个功能?
ArrayList <ArrayList<Character>> board = new ArrayList(); // this initialization is not
// wrong using Java 8 but for earlier versions you would need to state the type on both
//sides of the equal sign not just the left
for (int i = 0; i < board.size(); i++){
for (int j = 0; j < board.get(i).size(); j++){
board.get(i).get(j).add('[]');
}
}
但是那并不起作用。
它不必完全像这样,我只是想了解如何处理多维ArrayList。
-谢谢