如何开始通过从ArrayList中集索引迭代? [英] How to start iterating through ArrayList from set index?

查看:140
本文介绍了如何开始通过从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());
}

(内循环,以保持迭代范围)

or with for (to keep iterator scoped inside loop)

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


  • 或正常的循环,但开始从 I = 100

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


  • 或只是创建子列表,并遍历它就像你通常做

  • 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天全站免登陆