Add object to ArrayList at specified index

前端 未结 14 1419
南笙
南笙 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

    I draw your attention to the ArrayList.add documentation, which says it throws IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())

    Check the size() of your list before you call list.add(1, object1)

提交回复
热议问题