0

I want to dynamically create ArrayLists inside a loop like:

for (i = 0; i < 3; i++) {
    List<myItems> d(i) = new ArrayList<myItems>();
}

All I want to do is create 3 such lists inside a loop. Can I do that?

7
  • 2
    Yes, you can do that (you accidentally typed new twice) but it's not accomplishing anything. You're just creating a new List every time through the loop without using it. What exactly are you trying to accomplish? Commented Jun 26, 2013 at 18:13
  • 2
    "Can I do that?" You have my permission. Commented Jun 26, 2013 at 18:14
  • what do you mean by dynamic arraylist? Commented Jun 26, 2013 at 18:16
  • @jahroy I want to create 3 different arraylist's like d1,d2 and d3 inside a loop. Commented Jun 26, 2013 at 18:18
  • Your current code creates 3 different ArrayLists, it just assigns them all to the same variable, so each one gets overwritten. If you want 3 different Lists, you will need 3 different variables. Commented Jun 26, 2013 at 18:21

4 Answers 4

3

Maybe this is what you are looking for.

ArrayList<myItem>[] d = (ArrayList<myItem>[]) new ArrayList[3];

for (int i = 0; i < 3; i++) {
    d[i] = new ArrayList<myItems>();
}
Sign up to request clarification or add additional context in comments.

Comments

1

No: variable names are merely a convenience for the programmer and are not kept track of after your code is compiled, so that kind of "dynamic variable naming" is not allowed. You will have to find another way to keep track of your lists, such as a Map<String, List<Item>> that maps string identifiers to lists:

Map<String, List<Item>> map = new HashMap<String, List<Item>>()
map.put("d1", new ArrayList<Item>());
...

To then access the list corresponding to "d1", for example you can just use map.get("d1").

Comments

0

Have a List<List<MyItem>> instead and fill it in the loop

Comments

0

If you do this:

for(i=0;i<3;i++){
  List<myItems> d = new ArrayList<myItems>();
}

You will create a new array list three times. But each time you will store it to a newly created reference d and once you leave the scope of the for loop, that reference will vanish.

What you may be looking for is this:

List<List<myItems>> d = new ArrayList<List<myItems)>();
for(i=0;i<3;i++){
  d.add(new ArrayList<myItems>());
}

This creates a nested list:

someListOfListsOfMyItems
  subListOfMyItems
    someMyItem
    anotherMyItem
  subListOfMyItems
    someOtherMyItem
    anotherOtherMyItem
  subListOfMyItems
    yetSomeOtherMyItem
    yetAnotherMyItem

Each sublist can be accessed like so:

d.get(indexOfSublist);

2 Comments

how to add new item to the sublist? @Nathaniel Ford
d.get(idx).add(aNewItem);, where idx is the index of the sublist you're adding to. @ohlala

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.