Reputation: 6176
How can I create a two-dimensional array containing ArrayLists? Something like this :
ArrayList<Character>[][] myArray = new ArrayList<Character>[][];
and would it be ok to do the following :
I need to compare the position of some characters with the position of the buildings in my map. Several buildings can belong to the same tile, but one can be drawn in front of the character and the other behind him. This comparison has to be done all the time in the game, with every character.
I am trying to update an array of characters each time a character is moving from one tile to another. Then the render method should look for how many characters, if any, are in a specific tile, and loop over the characters in this tile to draw them in front or behind the buildings.
Something like this :
//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);
}
}
}
}
Upvotes: 2
Views: 4010
Reputation: 95
You could make a class and then make Objects using that class that stores an ArrayList<Character>
as an instance variable.
First make a class that has a instance variable ArrayList<Character>
that also has a getter, setter and constructor.
//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;
}
}
You can now use this class to make Objects and store that Objects in a 2D array
These Objects can store and ArrayList<Character>
that can be used.
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
Here are some ways you can use this 2D-Array
of 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);
}
Upvotes: 1
Reputation: 25950
I'd use list of lists, which is more dynamic.
List<ArrayList<Character>> list =
new ArrayList<ArrayList<Character>>();
Upvotes: 0
Reputation: 7494
A two dimensional array is an array of arrays - it means that the structure looks something like:
[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
Notice how the number of elements in each sub array does not have to be the same. You could create the above array as (I am using integers your type would vary as necessary):
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
So if had to do the same thing with an ArrayList, an array inside an array would translate to a list within a list. So you could do something like:
ArrayList<ArrayList<Integer>> arrayList = new ArrayList<ArrayList<Integer>>();
Since an ArrayList object can expand dynamically, the structure would be something like:
Row [0] -> [0][1][2]..... // and so on
Row [1] -> [0][1][2]..... // and so on
Row [2] -> [0][1][2]..... // and so on
Entering elements into this would be done very similarly using nested for loops.
Upvotes: 1
Reputation: 3389
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);
}
}
Upvotes: 4