Getting the last three elements from a List / ArrayList?
6 answers
You can use List.subList()
to get an idea of the tail of the original list:
List<E> tail = l.subList(Math.max(l.size() - 3, 0), l.size());
Here Math.max()
takes care of the case where it l
contains less than three elements.
+26
source to share
Use List.subList
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/List.html#subList%28int,%20int%29
Something like
myList.subList(myList.size()-3, myList().size());
+2
source to share
how about subList(from, to)
?
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/AbstractList.html#subList(int , int)
+1
source to share