加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何只从Arraylist中获取特定值

发布时间:2020-12-15 08:28:53 所属栏目:Java 来源:网络整理
导读:我有一个ArrayList Integer值(20,40,60,80,100,120)是否可以仅检索位置2-5,即60,100和120?谢谢你的帮助. for (DataSnapshot price : priceSnapshot.getChildren()) { int pr = price.getValue(Integer.class); priceList.add(pr); // size is 61 }int total
我有一个ArrayList< Integer>值(20,40,60,80,100,120)是否可以仅检索位置2-5,即60,100和120?谢谢你的帮助.

for (DataSnapshot price : priceSnapshot.getChildren()) {
    int pr = price.getValue(Integer.class);
    priceList.add(pr); // size is 61 
}
int total = 0;
List<Integer> totalList = 
            new ArrayList<Integer>(priceList.subList(29,34));               
for (int i = 0; i < totalList.size(); i++) {
    int to = totalList.get(i);
    total += to;
    txtPrice.setText(String.valueOf(total));
}

解决方法

在Java中,您可以创建列表的子列表( javadoc);例如

List<Integer> list = ...
List<Integer> sublist = list.sublist(2,6);

笔记:

>上限是独占的,因此要获得包含120的列表元素,我们必须指定6作为上限,而不是5.
>生成的子列表由原始列表“支持”.因此:

>创建子列表时不涉及复制,并且>更改子列表将修改原始列表中的相应位置.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读