如何创建包含ArrayLists的二维数组?

时间:2014-03-05 17:37:31

标签: java arrays arraylist libgdx

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

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

并且可以执行以下操作:

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

每次角色从一个磁贴移动到另一个磁贴时,我都会尝试更新一个字符数组。然后,渲染方法应查找特定图块中有多少个字符(如果有),并循环显示此图块中的字符以在建筑物的前面或后面绘制它们。

这样的事情:

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

    }
}

4 个答案:

答案 0 :(得分:1)

我会使用更有活力的列表列表。

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

答案 1 :(得分: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 :(得分:1)

二维数组是一个数组数组 - 它意味着结构类似于:

[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循环将输入元素非常相似。

答案 3 :(得分:0)

您可以创建一个类,然后使用该类存储ArrayList<Character>作为实例变量来创建对象。

首先创建一个具有实例变量ArrayList<Character>的类 还有一个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;
        }
    } 

您现在可以使用此类制作对象并将该对象存储在2D数组中 这些对象可以存储和ArrayList<Character>可以使用。

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中的ArrayList<Character>

的一些方法
//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);
    }