add objects with different name through for loop

前端 未结 6 992
北海茫月
北海茫月 2020-12-10 16:18

What is the best way to do the following:

List list = new LinkedList();

for(int i=0; i<30;i++)
{
  MyObject o1 = new MyOb         


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-10 17:03

    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 list = new LinkedList();
    
    for(int i=0; i<30;i++)
    {
    list.add(new MyObject());
    }
    

提交回复
热议问题