7

What is the best way to do the following:

List<MyObject> list = new LinkedList<MyObject>();

for(int i=0; i<30;i++)
{
  MyObject o1 = new MyObject();
  list.add(o1);
}

But the things is I don't wanna create objects with same name, I wanna create them with different name like o1,o2,o3,o4,o5,o6,o7,o8,o9,o10 and I wanna add each to the list. What is the best way to do this ?

7
  • 2
    Sorry, but what do you mean with name? The name of the local variable is removed by the compiler. Commented Mar 12, 2010 at 13:12
  • 2
    The big question here is why? The name of the variable in this case won't survive out past the forloop (or even into the next iteration of the forloop) in any case. How / Why is its name important? Commented Mar 12, 2010 at 13:12
  • 1
    Why do you need to have the variable name of your object different inside a loop? Variable names in the example you give is irrelevant... Commented Mar 12, 2010 at 13:12
  • If you want access to 10 variable names, you have to write 10 lines, you can't use a loop. Commented Mar 12, 2010 at 13:14
  • 2
    Others have hinted at it before, but I'd like to clarify: Objects don't have names! Just because you reference an object from a variable with a given name, doesn't mean that the object has that name. Commented Mar 12, 2010 at 13:28

6 Answers 6

12

You don't need to use a different name for each object. Since the o1 object is declared within the for loop, the scope of the o1 variable is limited to the for loop and it is recreated during each iteration ... except each time it will refer to the new object created during that iteration. Note that the variable itself is not stored within the list, only the object that it is referring to.

If you don't need to do anything else with the new object other than add it to the list, you can do:

for(int i=0; i<30;i++)
{
  list.add(new MyObject());
}
Sign up to request clarification or add additional context in comments.

1 Comment

@Younes: I'm not seeing why not. @Stephen: Re "...the o1 object is declared within the loop, it's (sic) scope is limited to the for loop..." That's mildly incorrect in an important way. The variable o1 is scoped to the loop; the object is not, provided some reference to it survives outside the loop (as it does in this case, on the list).
8

Why would you like to give them different names? You're creating the object inside the for loop, so it doesn't exist outside, and I therefor see noe reason to give them different names.

To answer your question: The only way I see, but I might be wrong, is to make an array of MyObjects and do:

List<MyObject> list = new LinkedList<MyObject>();
MyObject[] o = new MyObject[30];

for(int i = 0; i < 30; i++) {
    o[i] = new MyObject();
    list.add(o[i]);
}

1 Comment

That would be my answer, but saying that the object doesn't exist outside the loop is wrong, it's just the variable that is invalid outside the loop. The object are still (referenced) in the list.
2

Within the context of your loop, you don't need to worry about coming up with a name for each new instance you create. It's enough to say

List<MyObject> list = new LinkedList<MyObject>();

for(int i=0; i<30;i++)
{
list.add(new MyObject());
}

Comments

2

What you could do (though that wouldn't make much sense either), is to use a Map instead of a List

Map<String, MyObject> map = new HashMap<String, MyObject>();

for (int i = 0; i < 10; i++) {
    map.put("o" + i, new MyObject());
}

Comments

1

Your objects don't have names. The variable o1 has a name, but it's not linked to the object except that the variable refers to the object. The object in the list has no knowledge of ever having been referenced by the variable o1.

For what you're doing, you don't need a variable at all, as Stephen said in his answer you can just add the objects directly:

for (int i=0; i<30;i++)
{
    list.add(new MyObject());
}

Comments

-1

Create the object give it a name, add your constructors. then add the object name to the arraylist.

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.