0

I need to create 50 arraylists but rather than having to initialise them all individually. Is there a way in JAVA that I can do this dynamically??

i.e.

pseudo code:

for int i=1; i<51; i++
List<String> Counti = new ArrayList<String>();

So that in the loop it goes through and create 50 arraylists all called Count1, Count2, Count3 etc up until Count50.

I've tried creating a string and then naming the list by the string but it doesnt seem to recognise that teh name is a variable.

e.g.

for int i=1; i<51; i++
String Name="Count "+i
List<String> Name = new ArrayList<String>();

Instead it just creates a list called "Name"

Helen
  • 1
  • possible duplicate of [Can I declare and define variables with generated names and values?](http://stackoverflow.com/questions/26277156/can-i-declare-and-define-variables-with-generated-names-and-values) – LionC Nov 26 '14 at 14:33
  • Why you just trying to change naming i think you are adapting from javascript :) , create lists and add another list in java way. – İlker Korkut Nov 26 '14 at 14:34
  • possible duplicate of [Assigning variables with dynamic names in Java](http://stackoverflow.com/questions/6729605/assigning-variables-with-dynamic-names-in-java) – Raedwald Nov 30 '14 at 13:21

4 Answers4

3

You can do this with reflection, but this is a pretty bad idea. What you probably want to do is create an arraylist of arraylists.

 ArrayList<ArrayList<String>> listOfLists = new ArrayList<ArrayList<String>>();
 for (int i = 0; i < 50; i++)
      listOfLists.add(new ArrayList<String>());
wvdz
  • 16,251
  • 4
  • 53
  • 90
  • Consider using List> instead of the implementation class as a type, as it is easier to adapt in the future and makes sure that you only rely on the List Interface. – LionC Nov 27 '14 at 08:43
  • Prove me wrong, but as far as I know you can't do List> lists = new ArrayList>(); @LionC – Pier-Alexandre Bouchard Nov 27 '14 at 13:58
  • @Pier-AlexandreBouchard List> lists = new ArrayList<>(); works fine for me. You can then add as many (even different implementations) List as you like to it. The error in your line is the second ArrayList, which has to be a simple List, so that the type matches. – LionC Nov 27 '14 at 15:16
1

You should store them in a List and not create 50 variables

List<List<String>> lists = new ArrayList<ArrayList<String>>();
for (int i = 0; i < 50; i++)
{
    lists.add(new ArrayList<String>());
}
ToYonos
  • 16,469
  • 2
  • 54
  • 70
1

You need a List of ArrayList.

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

for (int i = 0; i < 50; i++){
    lists.add(new ArrayList<String>());
}
Pier-Alexandre Bouchard
  • 5,135
  • 5
  • 37
  • 72
  • I guess Stefan noticed the same – Robert3452 Nov 26 '14 at 14:40
  • Okay, maybe I should elaborate further, im using this as part of a larger piece of code where I am: – Helen Nov 26 '14 at 15:07
  • OK? And this should work for the part of larger piece of code where you are. – Pier-Alexandre Bouchard Nov 26 '14 at 15:09
  • im using this as part of a larger piece of code: creating an List of length 50 within a for loop (each result different). After each iteration store the results creating 50 rows of arraylists. Be able to do some processing at the end E.g. pick out element 0 of each arraylist. I tried creating a List of arraylists but it only outputs a list of 50 arraylists with the same results. I think after each iteration the array lists are updated. I want the first item in the list of array lists to be as at after the first iteration of the for loop. I cant think of another way of doing this? – Helen Nov 26 '14 at 15:22
  • Im a coding beginner so my code is really cluncky and trying to keep up with the logic behind stuff. Most of the code outputs what I want but in a really inefficient way! except I cant get the 50 different arraylists and be able to process them all. – Helen Nov 26 '14 at 15:23
  • Consider using List> instead of the implementation class as a type, as it is easier to adapt in the future and makes sure that you only rely on the List Interface. Also List is spelled with a capital L :-) – LionC Nov 27 '14 at 08:42
0

one another possible solution is to use a HashMap like this to name the ArrayLists:

Map<String, ArrayList<String>> mAllArrayListsByName = new HashMap<String, ArrayList<String>>();
for (int i = 0 ; i < 50 ; i++){
  mAllArrayListsByName.put("List"+String.valueOf(i), new ArrayList<String>());
}
ArrayList<String> lMyArrayList = mAllArrayListsByName.get("List0"); // get List 0
nano_nano
  • 12,351
  • 8
  • 55
  • 83