Add object to ArrayList at specified index

前端 未结 14 1450
南笙
南笙 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:26

    I think the solution from medopal is what you are looking for.

    But just another alternative solution is to use a HashMap and use the key (Integer) to store positions.

    This way you won't need to populate it with nulls etc initially, just stick the position and the object in the map as you go along. You can write a couple of lines at the end to convert it to a List if you need it that way.

提交回复
热议问题