我有一个值为ArrayList<Integer>的(20, 40, 60, 80, 100, 120),是否可以只检索位置2-5,也就是60, 80, 100 and 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));
}发布于 2019-03-03 07:50:44
在Java中,您可以创建一个列表的子列表(javadoc)。
List<Integer> list = ...
List<Integer> sublist = list.sublist(2, 6);备注:
120的list元素,我们必须指定6作为上限,而不是5。- there is no copying involved in creating the sublist, and
- changes to the sublist will modify the corresponding positions in the original list.
https://stackoverflow.com/questions/54966526
复制相似问题