简体   繁体   English

Java-像二维数组一样填充多维(2d)ArrayList

[英]Java - Filling multidimensional (2d) ArrayList like a 2d array

A while ago before I got used to object object oriented programming I created a basic TicTacToe game and to create the board I used an array. 前一阵子,我习惯了面向对象的编程之前,我创建了一个基本的TicTacToe游戏,并使用数组创建了棋盘。

The code is a complete mess because I didn't properly understand how to use objects, but I did initialize the board correctly: 该代码是一团糟,因为我没有正确理解如何使用对象,但是我确实正确地初始化了板子:

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
    }
}

My question is how would you this with an ArrayList? 我的问题是,如何使用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('[]');
                }
            }

but that does not work. 但这不起作用。

It does not have to be exactly like this, I just generally want to understand how to handle multidimensional ArrayLists. 它不必完全像这样,我通常只想了解如何处理多维ArrayList。

-thanks -谢谢

Unlike arrays, you can't initialize an entire ArrayList directly. 与数组不同,您不能直接初始化整个ArrayList。 You can specify the expected size beforehand (this helps performance when you are using very large lists, so it is a good practice to do it always). 可以事先指定期望的大小(在使用非常大的列表时,这有助于提高性能,因此始终执行此操作是一种很好的做法)。

int boardSize = 3;
ArrayList<ArrayList<Character>> board = new ArrayList<ArrayList<Character>>(boardSize);
for (int i = 0; i < boardSize; i++) {
    board.add(new ArrayList<Character>(boardSize));
    for (int j = 0; j < boardSize; j++){
        board.get(i).add('0');
    }
}

The main difference is that in your original code you had a multi-dimensional array of primitives (in this case, char ) and all you had to do was assign a new primitive value to each slot in the array. 主要区别在于,在原始代码中,您具有多维的原语数组(在本例中为char ),而您要做的就是为数组中的每个插槽分配一个新的原语值。

However what you want now is an ArrayList of ( ArrayList of Character ). 但是,您现在想要的是一个ArrayList of( Character ArrayList )。 When you create the ArrayList it is empty. 当您创建ArrayList它为空。 In order to procede you are going to need to fill it with several ( ArrayList of Character ) before you can begin to start adding Character s themselves. 为了继续进行,您将需要先用几个( ArrayList of Character )填充它,然后才能开始自己添加Character

So for example, 例如

ArrayList <ArrayList<Character>> board = new ArrayList<>();

for (int i=0; i<3; i++) {
  board.add(new ArrayList<Character>());
}

Now you can start adding Character s to your lists: 现在,您可以开始将Character添加到列表中:

for (int i=0; i<3; i++) {
  for (int j=0; j<3; j++) {
    board.get(i).add('A');
  }
}

Hope this helps. 希望这可以帮助。

First you have to initialize the ArrayList in your first line correctly and than you have to initialize an new ArrayList in each run of your first loop: 首先,您必须正确地在第一行中初始化ArrayList,然后在第一次循环的每次运行中都必须初始化一个新的ArrayList:

ArrayList <ArrayList<Character>> board = new ArrayList<ArrayList<Character>>();
for (int i = 0; i < 3; i++){
    ArrayList<Character> innerList = new ArrayList<Character>();
    board.add(innerList);
    for (int j = 0; j < 3; j++){
        innerList.add('[');
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM