How to get the last value of an ArrayList

Jessy picture Jessy · Mar 26, 2009 · Viewed 749.5k times · Source

How can I get the last value of an ArrayList?

I don't know the last index of the ArrayList.


Johannes Schaub - litb picture Johannes Schaub - litb · Mar 26, 2009

The following is part of the List interface (which ArrayList implements):

E e = list.get(list.size() - 1);

E is the element type. If the list is empty, get throws an IndexOutOfBoundsException. You can find the whole API documentation here.