4
我正在为Collapse游戏制作2D Arraylist板,但现在只是做一个文本表示。我创建了该板,但是当我尝试用randomChar()填充它时,所有行都会获得相同的随机字符。 我在做什么错?2D ArrayList初始化行
public static void createBoard(int rSize, int cSize) {
ArrayList<Character> row = new ArrayList<Character>();
ArrayList<ArrayList<Character>> board = new ArrayList<ArrayList<Character>>();
for (int c = 0; c < cSize; c++) {
board.add(row);
}
for (int r = 0; r < rSize; r++) {
board.get(r).add(randomChar());
//row.add(randomChar());
// board.get(r).set(r, randomChar());
}
//prints out board in table form
for (ArrayList<Character> r : board) {
printRow(r);
}
System.out.println(board);
}
Aaaaah,好的。我想到了这一点,但并没有想到发生了这种情况。谢谢! –