6

I have a ArrayList of model class with some items in it. How to add new item to first position of ArrayList and shift others next to it.

Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
Akshay
  • 6,029
  • 7
  • 40
  • 59
  • down voted because question already been answered at; http://stackoverflow.com/questions/12949690/java-arrays-how-to-add-elements-at-the-beginning – JGerulskis Jun 09 '15 at 13:25

2 Answers2

34

You can use this method.

arraylist .add(0, object)
Krishna V
  • 1,801
  • 1
  • 14
  • 16
  • See http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#add%28int,%20E%29 – sp00m Jun 09 '15 at 13:25
4

AbstractList declares the following:

public void add(int location, E object)

So,

myArrayList.add(0, myObject);

Would add it an the top of the list.

Shai
  • 25,159
  • 9
  • 44
  • 67