How can I create a two-dimensional array containing ArrayLists?

arraylist of int() arrays
print 2d arraylist java
how to add elements to 2d arraylist in java
how to iterate two dimensional arraylist in java
array of arraylist
array of arraylist java example
arraylist of arraylist
two dimensional array in java example program

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 :

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
                characterInThisTile = index of each character in arrayOfCharacters[y][x] 
                spriteBatch.draw(characterInThisTile, x_pos, y_pos, tileWidth, tileHeight);

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

Multi Dimensional ArrayList in Java, We can represent the edges in a 2-D ArrayList by creating and populating an ArrayList of ArrayLists. First, let's create a new 2-D ArrayList: int� Two-dimensional arrays. To declare a two-dimensional array, you simply list two sets of empty brackets, like this: int numbers[][]; Here, numbers is a two-dimensional array of type int. To put it another way, numbers is an array of int arrays. To create the array, you use the new keyword and provide lengths for each set of brackets, as in this

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.

Array of ArrayList in Java, We often come across 2D arrays where most of the part in the array is empty. Since space is a huge problem, we try different things to reduce� The Two Dimensional Array in Java programming language is nothing but an Array of Arrays. In Java Two Dimensional Array, data stored in row and columns, and we can access the record using both the row index and column index (like an Excel File).

I'd use list of lists, which is more dynamic.

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

How to declare and Initialize two dimensional Array in Java with , How to create an array from ArrayList of String in Java (tutorial); 100+ Data Structure Problems from Interviews (questions); Top 30 Array-based Coding Problems� Java ArrayList of Object Array. If you are not sure about the type of objects in the array or you want to create an ArrayList of arrays that can hold multiple types, then you can create an ArrayList of an object array. Below is a simple example showing how to create ArrayList of object arrays in java.

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;
        public arrayOfChars(){
            this.list = new ArrayList<Character>();
        public ArrayList<Character> getList(){
            return this.list;
        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++) {

        //Add to a list
        myLists[2][5].getList().add(new Character('H'));

        //Set a list of characters
        ArrayList<Character> useThisList = new ArrayList<Character>();

2D Array List in Java - OpenGenus IQ, add(Object element) : It helps in adding a new row in our existing 2D arraylist where element is the element to be added of datatype of which ArrayList created. <� We often come across 2D arrays where most of the part in the array is empty. Since space is a huge problem, we try different things to reduce the space. One such solution is to use jagged array when we know the length of each row in the array, but the problem arises when we do not specifically know the length of each of the rows.

Two-Dimensional Arrays \, A two-dimensional array is really nothing more than an array of arrays (a size( 200,200); int cols = width; int rows = height; // Declare 2D array int[][] myArray� A two-dimensional array can also be used to store objects, which is especially convenient for programming sketches that involve some sort of "grid" or "board." The following example displays a grid of Cell objects stored in a two-dimensional array.

[PDF] Multi-dimensional Arrays and ArrayLists, Creating an empty two-dimensional array. // Create a matrix with. // 5 rows and 7 columns int[][] matrix = new int[5][7]. // Set element at second row and. Java ArrayList. The ArrayList class is a resizable array, which can be found in the java.util package.. The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one).

[PDF] Arrays in Java, We initialize an array with new, specifying the length; syntax: an array of 100 ints to random values of arrays; we describe a two-dimensional array as having . What I want to do is to have a two dimensional array that I can go through and just write out to the database. My problem is that I don't know one of the dimensions of the array. So I am trying to create an array that represents a single record and then store it in the ArrayList.

  • With the List alternative there should be another level to get something equivalent to a List[][] (i.e. two-dimension array containing lists): List<List<ArrayList<Character>>>
  • This may be good advice, as a comment, but it's not an answer to the question that was asked.
  • And it lacks a level as well: to get something equivalent to the question would be a List<List<ArrayList<Character>>>
  • Thanks, I read that Lists are slower than arrays, but it is maybe ok if I only call indexes, I don't know I will try.