如何获取数组列表的最后一个值?
当前回答
考虑空列表的一行代码如下:
T lastItem = list.size() == 0 ? null : list.get(list.size() - 1);
或者如果你不喜欢空值(性能不是问题):
Optional<T> lastItem = list.stream().reduce((first, second) -> second);
其他回答
数组将它们的大小存储在一个名为length的局部变量中。给定一个名为“a”的数组,您可以使用以下方法引用最后一个索引,而不需要知道索引值
(a.length-1)
要给最后一个索引赋值5,你可以使用:
[a.length-1] = 5;
考虑空列表的一行代码如下:
T lastItem = list.size() == 0 ? null : list.get(list.size() - 1);
或者如果你不喜欢空值(性能不是问题):
Optional<T> lastItem = list.stream().reduce((first, second) -> second);
你所需要做的就是使用size()来获取数组列表的最后一个值。 例如,如果你是整数的数组列表,那么为了得到最后一个值,你必须
int lastValue = arrList.get(arrList.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();
}
}
在Kotlin中,你可以使用最后的方法:
val lastItem = list.last()
推荐文章
- Intellij IDEA Java类在保存时不能自动编译
- 何时使用Mockito.verify()?
- 在maven中安装mvn到底做什么
- 不可变与不可修改的集合
- 如何在JSON中使用杰克逊更改字段名
- GSON -日期格式
- 如何从线程捕获异常
- 无法解析主机"<URL here>"没有与主机名关联的地址
- 如何在Java中打印二叉树图?
- String.format()在Java中格式化双重格式
- com.jcraft.jsch.JSchException: UnknownHostKey
- Java中的操作符重载
- 如何加速gwt编译器?
- 在Hibernate中重新连接分离对象的正确方法是什么?
- 应该……接住环内还是环外?