0

I use a variable class which stores an integer. If the integer is 13, I need 13 arraylists to be created. Any idea as to how I would do this?

This is what is in my while loop:

ArrayList<Integer[]> list + count = new ArrayList<>();

With the following:

ArrayList<ArrayList<Integer>> myLists = new ArrayList<ArrayList<Integer>>();

for(int i = 0; i < 4; i++) {
ArrayList<Integer> newList = new ArrayList<Integer>();
myLists.add(newList);
}

Let's say I need the integer 9 to go into the 3rd array list and the integer 3 to go to the 2nd array list. How would i do that?

4
  • What are you trying to accomplish by putting a + sign on the left side of an assignment? That isn't legal Java. Are you inventing your own syntax and hoping the compiler can figure it out? Commented Jan 9, 2017 at 2:05
  • I'm trying to get multiple array lists as stated above. I thought the + sign would make the array list names be (list1, list2 .. list13) Commented Jan 9, 2017 at 2:06
  • You can create an array of ArrayList (ArrayList<T>[]) or an ArrayList of ArrayList (ArrayList<ArrayList<T>>). I don't know what T is. You can create an array of 13 ArrayLists by saying new ArrayList<T>[13], but then you have to create each ArrayList in a loop, since this will be an array of nulls. Commented Jan 9, 2017 at 2:07
  • 3
    You cannot create variable names dynamically in Java. If you want n objects, you can create an array, ArrayList, or some other data structure. Don't do it by trying to create a large number of variables. Commented Jan 9, 2017 at 2:08

3 Answers 3

1

What are you trying to achieve?

You can create an ArrayList of ArrayLists. So something like:

ArrayList<ArrayList<Integer>> myLists = new ArrayList<ArrayList<Integer>>();

for(int i = 0; i < numOfLists; i++) {
    ArrayList<Integer> newList = new ArrayList<Integer>();
    myLists.add(newList);
}

That way you'll have myLists with all the lists you created.

Sign up to request clarification or add additional context in comments.

4 Comments

Let's say now I need to add the integer 3 to the 3rd ArrayList. How would I do that?
Right after you initialize newList, you could do: newList.add(i+1)
How would you know which ArrayList? Like how do I know which ArrayList is newList?
Well, I'm not exactly sure what you want to achieve, but you know which list is which because of the order you created them. So the first ArrayList would be accessed in myLists by doing something like myLists.get(0) and if you want the first element in the first ArrayList in myLists, you could do myLists.get(0).get(0).
0

By concatinating the variable to make another variable name is not possible. The compiler should already told you about this. You can accomplish this by using another ArrayList

List<List<Integer[]>> mainList = new ArrayList<List<Integer[]>>();
        for(int i = 0; i  < 13; i++){

            mainList.add(new ArrayList<Integer[]>());
        }

Comments

0

If you want to identify each of the ArrayList by a name you could use a Map.

Map<String, ArrayList<Integer[]>> listsMap = new HashMap<>();
for(int i = 0; i < 13; i++){
   listsMap.add("list" + i, new ArrayList<>());
}

Then, if you need to add an integer to a particular list you could do this:

listsMap.get("list3").add(new Integer[]{3});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.