Java 通过for循环添加不同名称的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2432866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
add objects with different name through for loop
提问by Gandalf StormCrow
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 ?
但问题是我不想创建具有相同名称的对象,我想创建具有不同名称的对象,o1,o2,o3,o4,o5,o6,o7,o8,o9,o10
并且我想将每个对象添加到列表中。做这个的最好方式是什么 ?
采纳答案by Stephen Doyle
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.
您不需要为每个对象使用不同的名称。由于 o1 对象是在 for 循环中声明的,因此 o1 变量的范围仅限于 for 循环,并且在每次迭代期间都会重新创建……除非每次它都会引用在该迭代期间创建的新对象。请注意,变量本身不存储在列表中,只存储它所引用的对象。
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());
}
回答by T.J. Crowder
Your objects don't have names. The variableo1
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
.
您的对象没有名称。该变量o1
都有一个名字,但它不是链接到对象除了变量引用的对象。列表中的对象不知道曾经被变量引用过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());
}
回答by razlebe
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());
}
回答by martiert
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.
为什么要给它们起不同的名字?您正在 for 循环内创建对象,因此它在外部不存在,因此我认为没有理由给它们不同的名称。
To answer your question: The only way I see, but I might be wrong, is to make an array of MyObjects and do:
回答你的问题:我看到的唯一方法,但我可能错了,是制作一个 MyObjects 数组并执行:
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]);
}
回答by helpermethod
What you could do (though that wouldn't make much sense either), is to use a Map instead of a List
你可以做的(尽管这也没有多大意义),是使用 Map 而不是 List
Map<String, MyObject> map = new HashMap<String, MyObject>();
for (int i = 0; i < 10; i++) {
map.put("o" + i, new MyObject());
}
回答by joeeyejoeyeoje
Create the object give it a name, add your constructors. then add the object name to the arraylist.
创建对象给它一个名字,添加你的构造函数。然后将对象名称添加到数组列表中。