add objects with different name through for loop

前端 未结 6 982
北海茫月
北海茫月 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
    2020-12-10 16:43

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

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

提交回复
热议问题