mardi 21 avril 2015

2D ArrayList initializing rows

I'm making a 2D Arraylist board for a Collapse game, but for now just doing a text representation. I create the board, but when I attempt to fill it up with randomChar(), all the rows are getting the same random characters. What am I doing wrong?

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);

    } 




Aucun commentaire:

Enregistrer un commentaire