如何从设置索引开始遍历 ArrayList? [英] How to start iterating through ArrayList from set index?

查看:25
本文介绍了如何从设置索引开始遍历 ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ArrayList,我想从索引 100 开始遍历它,直到结束.我该怎么做?

I have an ArrayList and I want to start iterating through it from, say, index 100 to the end. How do I do that?

推荐答案

有很多方法可以做到这一点.在这个例子中,我假设你的列表包含整数.

There are many ways to do this. In this examples I assume your list holds Integers.

  1. 你可以使用 ListIterator

  1. You can use ListIterator

ListIterator<Integer> it = list.listIterator(100);
while (it.hasNext()) {
    System.out.println(it.next());
}

或使用 for(保持迭代器在循环内的作用域)

or with for (to keep iterator scoped inside loop)

for (ListIterator<Integer> lit = list.listIterator(100); lit.hasNext();) {
    System.out.println(lit.next());
}

  • 或正常的 for 循环但从 i=100

    for (int i=100; i<list.size(); i++){
        System.out.println(list.get(i));
    }
    

  • 或者像往常一样创建 subList 并对其进行迭代

  • or just create subList and iterate over it like you normally do

    for (Integer i : list.subList(100, list.size())){
        System.out.println(i);
    }
    

  • 这篇关于如何从设置索引开始遍历 ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆