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"

3

4 Answers 4

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>());
Sign up to request clarification or add additional context in comments.

3 Comments

Consider using List<List<String>> 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.
Prove me wrong, but as far as I know you can't do List<List<String>> lists = new ArrayList<ArrayList<String>>(); @LionC
@Pier-AlexandreBouchard List<List<String>> lists = new ArrayList<>(); works fine for me. You can then add as many (even different implementations) List<String> 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.
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>());
}

2 Comments

This answer AND the previous answer from Pier both have the same mistake (missing closing bracket)
This snippet won't even compile.
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>());
}

6 Comments

I guess Stefan noticed the same
Okay, maybe I should elaborate further, im using this as part of a larger piece of code where I am:
OK? And this should work for the part of larger piece of code where you are.
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?
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.
|
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

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.