本文翻譯自:How to get the last value of an ArrayList
How can I get the last value of an ArrayList? 如何獲取ArrayList的最後一個值?
I don't know the last index of the ArrayList. 我不知道ArrayList的最後一個索引。
#1樓
參考:https://stackoom.com/question/2sw5/如何獲取ArrayList的最後一個值
#2樓
There isn't an elegant way in vanilla Java. 在香草Java中沒有一種優雅的方式。
Google Guava 谷歌番石榴
The Google Guava library is great - check out their Iterables
class . Google Guava庫很棒 - 查看他們的Iterables
類 。 This method will throw a NoSuchElementException
if the list is empty, as opposed to an IndexOutOfBoundsException
, as with the typical size()-1
approach - I find a NoSuchElementException
much nicer, or the ability to specify a default: 如果列表爲空,此方法將拋出NoSuchElementException
,而不是IndexOutOfBoundsException
,與典型的size()-1
方法一樣 - 我發現NoSuchElementException
更好,或者指定默認值的能力:
lastElement = Iterables.getLast(iterableList);
You can also provide a default value if the list is empty, instead of an exception: 如果列表爲空,您還可以提供默認值,而不是例外:
lastElement = Iterables.getLast(iterableList, null);
or, if you're using Options: 或者,如果您使用選項:
lastElementRaw = Iterables.getLast(iterableList, null);
lastElement = (lastElementRaw == null) ? Option.none() : Option.some(lastElementRaw);
#3樓
I use micro-util class for getting last (and first) element of list: 我使用micro-util類來獲取列表的最後一個(和第一個)元素:
public final class Lists {
private Lists() {
}
public static <T> T getFirst(List<T> list) {
return list != null && !list.isEmpty() ? list.get(0) : null;
}
public static <T> T getLast(List<T> list) {
return list != null && !list.isEmpty() ? list.get(list.size() - 1) : null;
}
}
Slightly more flexible: 稍微靈活一點:
import java.util.List;
/**
* Convenience class that provides a clearer API for obtaining list elements.
*/
public final class Lists {
private Lists() {
}
/**
* Returns the first item in the given list, or null if not found.
*
* @param <T> The generic list type.
* @param list The list that may have a first item.
*
* @return null if the list is null or there is no first item.
*/
public static <T> T getFirst( final List<T> list ) {
return getFirst( list, null );
}
/**
* Returns the last item in the given list, or null if not found.
*
* @param <T> The generic list type.
* @param list The list that may have a last item.
*
* @return null if the list is null or there is no last item.
*/
public static <T> T getLast( final List<T> list ) {
return getLast( list, null );
}
/**
* Returns the first item in the given list, or t if not found.
*
* @param <T> The generic list type.
* @param list The list that may have a first item.
* @param t The default return value.
*
* @return null if the list is null or there is no first item.
*/
public static <T> T getFirst( final List<T> list, final T t ) {
return isEmpty( list ) ? t : list.get( 0 );
}
/**
* Returns the last item in the given list, or t if not found.
*
* @param <T> The generic list type.
* @param list The list that may have a last item.
* @param t The default return value.
*
* @return null if the list is null or there is no last item.
*/
public static <T> T getLast( final List<T> list, final T t ) {
return isEmpty( list ) ? t : list.get( list.size() - 1 );
}
/**
* Returns true if the given list is null or empty.
*
* @param <T> The generic list type.
* @param list The list that has a last item.
*
* @return true The list is empty.
*/
public static <T> boolean isEmpty( final List<T> list ) {
return list == null || list.isEmpty();
}
}
#4樓
如果可以,可以將ArrayList
替換爲ArrayDeque
,它具有removeLast
等方便的方法。
#5樓
How about this.. Somewhere in your class... 怎麼樣......你班上的某個地方......
List<E> list = new ArrayList<E>();
private int i = -1;
public void addObjToList(E elt){
i++;
list.add(elt);
}
public E getObjFromList(){
if(i == -1){
//If list is empty handle the way you would like to... I am returning a null object
return null; // or throw an exception
}
E object = list.get(i);
list.remove(i); //Optional - makes list work like a stack
i--; //Optional - makes list work like a stack
return object;
}
#6樓
The last item in the list is list.size() - 1
. 列表中的最後一項是list.size() - 1
。 The collection is backed by an array and arrays start at index 0. 該集合由數組支持,數組從索引0開始。
So element 1 in the list is at index 0 in the array 因此列表中的元素1位於數組中的索引0處
Element 2 in the list is at index 1 in the array 列表中的元素2位於數組中的索引1處
Element 3 in the list is at index 2 in the array 列表中的元素3位於數組中的索引2處
and so on.. 等等..