如何获取ArrayList的最后一个值

我怎样才能得到一个ArrayList的最后一个值?

我不知道ArrayList的最后一个索引。

下面是List接口的一部分(哪个ArrayList实现):

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

E是元素types。 如果列表为空,则抛出IndexOutOfBoundsException 。 你可以在这里find整个API文档。

这应该做到这一点:

 if (arrayList != null && !arrayList.isEmpty()) { T item = arrayList.get(arrayList.size()-1); } 

Java中没有一种优雅的方式。

Google Guava

谷歌番石榴图书馆是伟大的 – 检查他们的Iterables类 。 如果列表是空的,这个方法将会抛出一个NoSuchElementException ,而不是像IndexOutOfBoundsException ,这与典型的size()-1方法一样 – 我发现NoSuchElementException更好,或者指定一个默认值:

 lastElement = Iterables.getLast(iterableList); 

如果列表是空的,也可以提供一个默认值,而不是一个例外:

 lastElement = Iterables.getLast(iterableList, null); 

或者,如果您使用选项:

 lastElementRaw = Iterables.getLast(iterableList, null); lastElement = (lastElementRaw == null) ? Option.none() : Option.some(lastElementRaw); 

我使用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; } } 

稍微更灵活:

 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(); } } 

size()方法返回ArrayList中元素的数量。 元素的索引值是0(size()-1) ,因此您可以使用myArrayList.get(myArrayList.size()-1)来检索最后一个元素。

如果可以的话,换掉一个ArrayDequeArrayList ,它具有方便的方法,如removeLast

  Let ArrayList is myList public void getLastValue(List myList){ // Check ArrayList is null or Empty if(myList == null || myList.isEmpty()){ return; } // check size of arrayList int size = myList.size(); // Since get method of Arraylist throws IndexOutOfBoundsException if index >= size of arrayList. And in arraylist item inserts from 0th index. //So please take care that last index will be (size of arrayList - 1) System.out.print("last value := "+myList.get(size-1)); } 

列表中的最后一项是list.size() - 1 。 该集合由数组支持,数组从数组索引0开始。

因此,列表中的元素1位于数组中的索引0处

列表中的元素2位于数组中的索引1处

列表中的元素3位于数组中的索引2处

等等..

所有你需要做的就是使用size()来获得Arraylist的最后一个值。 例如。 如果你是整数的ArrayList,那么得到最后的值,你将不得不

 int lastValue = arrList.get(arrList.size()-1); 

请记住,可以使用索引值访问Arraylist中的元素。 因此,ArrayLists通常用于search项目。

数组将其大小存储在名为“length”的局部variables中。 给定一个名为“a”的数组,您可以使用以下引用最后一个索引而不知道索引值

一个[则为a.length-1]

给这个最后一个索引分配一个值5:

一个[则为a.length-1] = 5;

这个怎么样?你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; } 

如果修改列表,然后使用listIterator()并从最后一个索引(即size()-1 )迭代。 如果再次失败,请检查您的列表结构。