Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Add item to top of arraylist [duplicate]

Tags:

java

arraylist

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.

like image 820
Akshay Avatar asked Jun 09 '15 13:06

Akshay


2 Answers

You can use this method.

arraylist .add(0, object)
like image 183
Krishna V Avatar answered Nov 02 '22 14:11

Krishna V


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.

like image 41
Shai Avatar answered Nov 02 '22 12:11

Shai