Java ArrayList how to add elements at the beginning

后端 未结 14 1586
忘了有多久
忘了有多久 2020-12-02 06:49

I need to add elements to an ArrayList queue whatever, but when I call the function to add an element, I want it to add the element at the beginning of the arra

14条回答
  •  忘掉有多难
    2020-12-02 07:25

    You can take a look at the add(int index, E element):

    Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

    Once you add you can then check the size of the ArrayList and remove the ones at the end.

提交回复
热议问题