34

I want to create a 2D array that each cell is an ArrayList!

I consider this defintions, but I can not add anything to them are these defintions true?!

ArrayList<ArrayList<String>> table = new ArrayList<ArrayList<String>>();

or

ArrayList[][] table = new ArrayList[10][10];

//table.add??????

Please help me

3
  • 1
    This will help you stackoverflow.com/questions/10768170/… Commented Jun 6, 2013 at 8:06
  • Hi. The title of the question is not consistent with its content. Do you want a 2D array of ArrayList (something like 3D, finally) or a 2D ArrayList (an ArrayList of ArrayList)? If you ask this for your homework, could you write the original question. Finally, do you absolutely need to declare ArrayList. Can you use list intead? Commented Jun 6, 2013 at 8:13
  • Oh and you declare 2D ArrayList of String in the first part of your question. Is that correct that you need to put String in your inner ArrayList? Commented Jun 6, 2013 at 8:16

5 Answers 5

61

I want to create a 2D array that each cell is an ArrayList!

If you want to create a 2D array of ArrayList.Then you can do this :

ArrayList[][] table = new ArrayList[10][10];
table[0][0] = new ArrayList(); // add another ArrayList object to [0,0]
table[0][0].add(); // add object to that ArrayList
Sign up to request clarification or add additional context in comments.

5 Comments

Compiler error only if arrayListObject is not an ArrayList object.
table [0][0] is null so fistly you need to initialize it.
Aside from the null side of things, this is the nicest solution we've seen on here. +1
Thanks, overlooked it.
Actually, I am getting a warning unchecked call to add(e) as a member of the raw type that the datatype is not specified for the inner arraylist, how to do that ?
52

The best way is to use a List within a List:

List<List<String>> listOfLists = new ArrayList<List<String>>();  

Comments

38

1st of all, when you declare a variable in java, you should declare it using Interfaces even if you specify the implementation when instantiating it

ArrayList<ArrayList<String>> listOfLists = new ArrayList<ArrayList<String>>();

should be written

List<List<String>> listOfLists = new ArrayList<List<String>>(size); 

Then you will have to instantiate all columns of your 2d array

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

And you will use it like this :

listOfLists.get(0).add("foobar");

But if you really want to "create a 2D array that each cell is an ArrayList!"

Then you must go the dijkstra way.

7 Comments

shouldn;t it be for(List<String> col : listOfLists )?
indeed :) feel free to edit
This is just false, you can't modify the listOfLists structure this way! You are iterating through an empty structure so the for will do nothing and the following get will then throw an exception.
indeed :) it needs a good old for
Hi, I see you specified the size within ( ), which is the size of ArrayList on the outer side. What should I do to specify the size of each inner ArrayList in the outer ArrayList?
|
3
ArrayList<String>[][] list = new ArrayList[10][10];
list[0][0] = new ArrayList<>();
list[0][0].add("test");

Comments

2

This can be achieve by creating object of List data structure, as follows

List list = new ArrayList();

For more information refer this link

How to create a Multidimensional ArrayList in Java?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.