如何获取数组列表的最后一个值?
当前回答
你所需要做的就是使用size()来获取数组列表的最后一个值。 例如,如果你是整数的数组列表,那么为了得到最后一个值,你必须
int lastValue = arrList.get(arrList.size()-1);
记住,数组列表中的元素可以通过下标值访问。因此,数组列表通常用于搜索项。
其他回答
如解决方案中所述,如果List为空,则抛出IndexOutOfBoundsException。一个更好的解决方案是使用Optional类型:
public class ListUtils {
public static <T> Optional<T> last(List<T> list) {
return list.isEmpty() ? Optional.empty() : Optional.of(list.get(list.size() - 1));
}
}
如你所料,列表的最后一个元素作为Optional返回:
var list = List.of(10, 20, 30);
assert ListUtils.last(list).orElse(-1) == 30;
它还可以优雅地处理空列表:
var emptyList = List.<Integer>of();
assert ListUtils.last(emptyList).orElse(-1) == -1;
在普通Java中没有优雅的方法。
谷歌番石榴
谷歌番石榴库是伟大的-检查他们的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);
这对我很管用。
private ArrayList<String> meals;
public String take(){
return meals.remove(meals.size()-1);
}
如果修改列表,则使用listIterator()并从最后一个索引(即分别为size()-1)开始迭代。 如果你再次失败,检查你的列表结构。
我使用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();
}
}
推荐文章
- 用于桌面应用程序的Swing vs JavaFx
- 为什么这个Java程序会终止,尽管它显然不应该(也没有)终止?
- 使用split("|")按管道符号拆分Java字符串
- 当内存不足导致抛出OutOfMemoryError时会发生什么?
- 检查ArrayList中是否存在值
- 我们能在Java中创建无符号字节吗
- 同步vs锁定
- 在ExecutorService的提交和执行之间进行选择
- 如何在javadoc中转义@字符?
- 如何在Java中创建唯一的ID ?
- 为什么Java类的编译与空行不同?
- Android Studio无法找到有效的Jvm(与MAC OS相关)
- 为什么一个组合器需要减少方法转换类型在java 8
- CountDownLatch是如何在Java多线程中使用的?
- 如果我同步了同一个类上的两个方法,它们能同时运行吗?