3D-kreativ
3D-kreativ

Reputation: 9307

How to create a class to handle multidimensional arraylist

During my search for info about multidimensional arraylist, I read that you could create a class to handle the multidimensional arraylist.

I'm doing a Android game similar to Bejeweled, and instead of using a common multidimensional grid, I guess this would be better!? I also has a list of all the sprite objects and I wonder if I can store and control all this objects with a multidimensional arraylist class?

But I'm not sure how a class would look like and how do I set and get values from the arraylists? Is there anyone who are interested to explain how it's working and show a simple code example? Thanks!

EDIT: I want to have objects in the arraylists like this example int grid[][] = new int[8][8]; but instead of integers, I want to have objects. And I wonder if it's possible to get and set values the same easy was like in the example I just showed?

EDIT 2: I have a grid 8x8 and that's why I'm using a array like [8][8], but I can only have values of type integers or booleans at each positions. I also have to use an arraylist to handle all the 64 objects that has a positions within this grid. Each object store it's position, both in the grid array and the position on the screen. I just wanted to make it a little bit simplier to handle and create a gird with multidimensional arrayList to both store the objects and have a grid? Possible or is there a better way to do this?

Upvotes: 1

Views: 1025

Answers (4)

Gustek
Gustek

Reputation: 3760

There is no big difference between ArrayList and array.

Would say there are only 2 pros of ArrayList:

  • ability to dynamically change a size
  • adding new element at end of array, without need to look for first empty slot

And for multidimensional another pros is possibility to have different lengths for nested ArrayLists.

In Your case I think You don't need any of this so choose one You feel more comfortable, If You don't know how to make ArrayList of ArrayLists but know how to use array use them.

But just to answer Your question.

ArrayList<ArrayList<Object>> root = new ArrayList<ArrayList<Object>>();

root.add(new ArrayList<Object>());
root.add(new ArrayList<Object>());
root.add(new ArrayList<Object>());
root.add(new ArrayList<Object>());

root.get(1).set(1, Object); //puts new object at position 1,1

Object o = root.get(1).get(1); //gets object from position 1,1

With multidimensional array:

Object[][] grid = new Object[8][8];

grid[1][4] = new Object(); //set new object at position 1,4

grid[1][4].doSmth(); //calls method doSmth() of object stored at position 1,4

Object o = grid[1][4]; //gets object at position 1,4 

Upvotes: 1

Marco Forberg
Marco Forberg

Reputation: 2644

First your start with defining your multi-dimensional arrayList

int size = 8;
List<List<YourClass>> 2dList = new ArrayList<List<YourClass>>(size);

then you initialize it

for(int rowIndex = 0; rowIndex < size; rowIndex++) {
    List<YourClass> row = new ArrayList<YourClass>(size)

    for(int columnIndex = 0; columnIndex < size; columnIndex++) {
        row.add(new YourClass());
    }

    2dList.add(row);
}

now you have an sizexsize ArrayList with instances of your class

to access it:

2dList.get(rowIndex).get(columnIndex) // will return the YourClass-Object you placed there

if you want to replace the object on a certain position with another instance you need to remove the old object first:

2dList.get(rowIndex).remove(columnIndex);
2dList.get(rowIndex).add(columnIndex, newYourClass);

Upvotes: 0

Rahul Bobhate
Rahul Bobhate

Reputation: 5082

    int size =8;
    List<List<Integer>> list  = new ArrayList<List<Integer>>(size);

    for(int i=0; i<size; i++)
    {
        list.add(new ArrayList<Integer>());
    }

    for (int i = 0; i < size; i++) 
    {
        for (int j = 0; j < size; j++) 
        {
            Integer x = list.get(0).get(1); //access the 0,1 element
        }
    }

EDIT :
If you want to use a specific class. For ex: Node, you could use List<List<Node>> instead of List<List<Integer>> and then access it. You can then call any methods of the Node class as follows:

 Node node = list.get(0).get(1); // access the Node at 0,1
 node.getProperties();

Upvotes: 2

sanbhat
sanbhat

Reputation: 17622

List<List<Integer>> multiList = new ArrayList<List<Integer>>();
multiList.add(new ArrayList<Integer>());
Integer element = multi.get(0).get(0); // element at 0,0

Upvotes: 0

Related Questions