【问题标题】:How can I create a two-dimensional array containing ArrayLists?如何创建包含 ArrayLists 的二维数组?
【发布时间】:2014-03-05 17:37:31
【问题描述】:

如何创建包含 ArrayLists 的二维数组?像这样的东西:

ArrayList<Character>[][] myArray = new ArrayList<Character>[][];

是否可以执行以下操作:

我需要将某些角色的位置与地图中建筑物的位置进行比较。多个建筑物可以属于同一个图块,但可以在角色前面绘制一个,在他后面绘制另一个。在游戏中,每个角色都必须始终进行这种比较。

每次一个字符从一个磁贴移动到另一个磁贴时,我都会尝试更新一组字符。然后,render 方法应该查找特定图块中有多少字符(如果有),并遍历该图块中的字符以将它们绘制在建筑物的前面或后面。

类似这样的:

//init
ArrayList<Character>[][] arrayOfCharacters = new ArrayList<Character>[][];

//each tile in the map
for (int y = 0; y < 9; y++){
    for(int x = 9-1; x >= 0; x--){
        if ( arrayOfCharacters[y][x].length > 0 ){
            for ( int i=0, i< arrayOfCharacters[y][x].length; i++ ){
                //compare which building is in front or behind the characters
                //then
                characterInThisTile = index of each character in arrayOfCharacters[y][x] 
                spriteBatch.draw(characterInThisTile, x_pos, y_pos, tileWidth, tileHeight);
            }   
        }

    }
}

【问题讨论】:

    标签: java arrays arraylist libgdx


    【解决方案1】:
    ArrayList<Character>[][] arrayOfCharacters = new ArrayList[9][9];
    for(int i=0;i<arrayOfCharacters.length;i++){
          for(int i2=0;i2<arrayOfCharacters[i].length;i2++){
                arrayOfCharacters[i][i2]=new ArrayList<Character>(20);
          }
    }
    

    【讨论】:

      【解决方案2】:

      二维数组是数组的数组 - 这意味着结构看起来像:

      [0,0][0,1][0,2][0,3] -> sub array 1
      [1,0][1,1][1,2] -> sub array 2
      [2,0][2,1][2,2][2,3][2,4] - sub array 3
      

      注意每个子数组中的元素数量不必相同。您可以将上述数组创建为(我使用的是整数,您的类型会根据需要而变化):

      int[][] a = new int[3][]; // The number of sub arrays or the first argument should be defined. 
      // The number of elements in each sub array need not be known at compile time though
      

      因此,如果必须对 ArrayList 执行相同的操作,则数组中的数组将转换为列表中的列表。所以你可以这样做:

      ArrayList<ArrayList<Integer>> arrayList = new ArrayList<ArrayList<Integer>>();
      

      由于 ArrayList 对象可以动态扩展,因此结构类似于:

      Row [0] -> [0][1][2]..... // and so on
      Row [1] -> [0][1][2]..... // and so on
      Row [2] -> [0][1][2]..... // and so on
      

      使用嵌套的 for 循环非常相似地向其中输入元素。

      【讨论】:

      • 使用List 替代方案应该有另一个级别来获得与List[][] 等效的东西(即包含列表的二维数组):List&lt;List&lt;ArrayList&lt;Character&gt;&gt;&gt;
      【解决方案3】:

      您可以创建一个类,然后使用该类将 ArrayList&lt;Character&gt; 存储为实例变量来创建对象。

      首先创建一个具有实例变量ArrayList&lt;Character&gt;的类 它还有一个 getter、setter 和构造函数。

      //Make Objects that will have an ArrayList<Character>
          public class ArrayOfChars {
              private ArrayList<Character> list;
              //Constructor
              public arrayOfChars(){
                  this.list = new ArrayList<Character>();
              }
              //Getter
              public ArrayList<Character> getList(){
                  return this.list;
              }
              //Setter
              public void setList(ArrayList<Character> list){
                  this.list = list;
              }
          } 
      

      您现在可以使用这个类来制作对象并将这些对象存储在二维数组中 这些Objects可以存储和ArrayList&lt;Character&gt;可以使用。

      public static void main(String[] args) {
              ArrayOfChars[][] myLists = new ArrayOfChars[9][9];
              //initialize the 2d array so that it is filled with Empty ArrayList<>'s'
              for (int i = 0; i < 9; i++) {
                  for (int j = 0; j < 9; j++) {
                      ArrayOfChars thislist = new ArrayOfChars();
                      myLists[i][j] = thislist;
                  }
              }
              //You can now use it like a 2d array of objects 
      

      这里有一些你可以使用这个2D-Array of ArrayList&lt;Character&gt;的方法

      //Iterate like this
              for (int row = 0; row < 9; row++) {
                  for (int col = 0; col < 9; col++) {
                      myLists[row][col].getList().get(index);
                      //or
                      myLists[row][col].setList(list);
                  }
              }
      
      
              //Add to a list
              myLists[2][5].getList().add(new Character('H'));
      
              //Set a list of characters
              ArrayList<Character> useThisList = new ArrayList<Character>();
              useThisList.add('F');
              useThisList.add('G');
              useThisList.add('L');
              myLists[3][7].setList(useThisList);
          }
      

      【讨论】:

        【解决方案4】:

        我会使用更动态的列表列表。

        List<ArrayList<Character>> list = 
        new ArrayList<ArrayList<Character>>();
        

        【讨论】:

        • 作为评论,这可能是一个很好的建议,但它不是对所提问题的答案
        • 而且它也缺少一个级别:要获得与问题等效的内容将是List&lt;List&lt;ArrayList&lt;Character&gt;&gt;&gt;
        • 谢谢,我读到Lists比数组慢,但如果我只调用索引可能还可以,我不知道我会尝试。
        猜你喜欢
        • 2020-12-20
        • 2021-10-12
        • 1970-01-01
        • 2013-01-30
        • 1970-01-01
        • 2011-12-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多