【发布时间】:2015-08-13 05:55:41
【问题描述】:
不久前,在我习惯于面向对象编程之前,我创建了一个基本的井字游戏并使用数组来创建棋盘。
代码完全是一团糟,因为我没有正确理解如何使用对象,但我确实正确初始化了板子:
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('[]');
}
}
但这不起作用。
不一定要完全这样,我只是一般想了解如何处理多维ArrayLists。
-谢谢
【问题讨论】:
标签: java arrays multidimensional-array arraylist