Add object to ArrayList at specified index

前端 未结 14 1435
南笙
南笙 2020-11-30 18:59

I think it\'s a fairly simple question, but I can\'t figure out how to do this properly.

I\'ve got an empty arraylist:

ArrayList list =         


        
      
      
      
14条回答
  •  时光说笑
    2020-11-30 19:46

    You need to populate the empty indexes with nulls.

    while (arraylist.size() < position)
    {
         arraylist.add(null);
    }
    
    arraylist.add(position, object);
    

提交回复
热议问题