32

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

Prateek Gupta
  • 119
  • 2
  • 7
Rezan
  • 351
  • 1
  • 4
  • 5
  • 1
    This will help you http://stackoverflow.com/questions/10768170/how-do-i-declare-a-2d-string-arraylist – newuser Jun 06 '13 at 08: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? – C.Champagne Jun 06 '13 at 08: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? – C.Champagne Jun 06 '13 at 08:16

5 Answers5

60

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
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
51

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

List<List<String>> listOfLists = new ArrayList<List<String>>();  
Can
  • 4,516
  • 6
  • 28
  • 50
PSR
  • 39,804
  • 41
  • 111
  • 151
35

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.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
Gab
  • 7,869
  • 4
  • 37
  • 68
  • shouldn;t it be for(List col : listOfLists )? – Saba Jamalian Dec 06 '14 at 04:19
  • indeed :) feel free to edit – Gab Dec 06 '14 at 21:18
  • 1
    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. – Jean-Baptiste Yunès Dec 08 '14 at 18:19
  • indeed :) it needs a good old for – Gab Dec 08 '14 at 21:20
  • 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? – tonyabracadabra Jun 11 '15 at 13:07
  • in the initialisation for loop. – Gab Jun 11 '15 at 15:01
  • "when you declare a variable in java, you should declare it using Interfaces even if you specify the implementation when instantiating it".. @Gab I'm not sure I understand that. Why would one do that? Doesn't that limit you to calling only those methods on the variable which are contained in the interface? It also forces explicit type casting in case you want the additional methods the implementation class provides. So I'd like to understand why you suggested that – Ayush Aug 21 '20 at 12:01
3
ArrayList<String>[][] list = new ArrayList[10][10];
list[0][0] = new ArrayList<>();
list[0][0].add("test");
dijkstra
  • 1,068
  • 2
  • 16
  • 39
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?

Community
  • 1
  • 1
Rachana K
  • 104
  • 1
  • 4