Java 使用Iterator时如何获取当前循环索引?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3329842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:14:12  来源:igfitidea点击:

How to get the current loop index when using Iterator?

javaiterator

提问by Mahmoud Saleh

I am using an Iterator to iterate through a collection and I want to get the current element's index.

我正在使用 Iterator 迭代一个集合,我想获取当前元素的索引。

How can I do that?

我怎样才能做到这一点?

采纳答案by Chris Diver

Use your own variable and increment it in the loop.

使用您自己的变量并在循环中递增它。

回答by Florian Reischl

Use an int and increment it within your loop.

使用 int 并在循环中增加它。

回答by Mateusz Dymczyk

What kind of collection? If it's an implementation of the List interface then you could just use it.nextIndex() - 1.

什么样的收藏?如果它是 List 接口的实现,那么您可以使用it.nextIndex() - 1.

回答by Jatin

Use a ListIteratorto iterate through the Collection. If the Collection is not a List to start with use Arrays.asList(Collection.toArray())to turn it into a List first.

使用ListIterator遍历集合。如果 Collection 不是 List 开始使用Arrays.asList(Collection.toArray()),请先将其转换为 List。

回答by Jolly

See here.

这里

iterator.nextIndex()would provide index of element that would be returned by subsequent call to next().

iterator.nextIndex()将提供后续调用将返回的元素索引next()

回答by Tom Clift

Here's a way to do it using your own variable and keeping it concise:

这是一种使用您自己的变量并保持简洁的方法:

List<String> list = Arrays.asList("zero", "one", "two");

int i = 0;
for (Iterator<String> it = list.iterator(); it.hasNext(); i++) {
    String s = it.next();
    System.out.println(i + ": " + s);
}

Output (you guessed it):

输出(你猜对了):

0: zero
1: one
2: two

The advantage is that you don't increment your index within the loop (although you need to be careful to only call Iterator#next once per loop - just do it at the top).

优点是您不会在循环内增加索引(尽管您需要小心,每个循环只调用一次 Iterator#next - 只需在顶部执行)。

回答by Paul

I had the same question and found using a ListIterator worked. Similar to the test above:

我有同样的问题,发现使用 ListIterator 有效。类似于上面的测试:

List<String> list = Arrays.asList("zero", "one", "two");

ListIterator iter = list.listIterator();

while (iter.hasNext()) {
    System.out.println("index: " + iter.nextIndex() + " value: " + iter.next());
}

Make sure you call the nextIndex BEFORE you actually get the next().

确保在实际获得 next() 之前调用 nextIndex。

回答by Robert Klemme

You can use ListIteratorto do the counting:

您可以使用以下ListIterator方法进行计数:

final List<String> list = Arrays.asList("zero", "one", "two", "three");

for (final ListIterator<String> it = list.listIterator(); it.hasNext();) {
    final String s = it.next();
    System.out.println(it.previousIndex() + ": " + s);
}

回答by Ryan

All you need to use it the iterator.nextIndex() to return the current index that the iterator is on. This could be a bit easier than using your own counter variable (which still works also).

您只需要使用 iterator.nextIndex() 来返回迭代器所在的当前索引。这可能比使用您自己的计数器变量(它仍然有效)更容易一些。

public static void main(String[] args) {    
    String[] str1 = {"list item 1", "list item 2", "list item 3", "list item 4"};
    List<String> list1 = new ArrayList<String>(Arrays.asList(str1));

    ListIterator<String> it = list1.listIterator();

    int x = 0;

    //The iterator.nextIndex() will return the index for you.
    while(it.hasNext()){
        int i = it.nextIndex();
        System.out.println(it.next() + " is at index" + i); 
    }
}

This code will go through the list1 list one item at a time and print the item's text, then "is at index" then it will print the index that the iterator found it at. :)

此代码将一次遍历 list1 列表一个项目并打印该项目的文本,然后“位于索引”然后它将打印迭代器找到它的索引。:)

回答by Sunny

just do something like this:

只是做这样的事情:

        ListIterator<String> it = list1.listIterator();
        int index = -1;
        while (it.hasNext()) {
            index++;
            String value = it.next();
            //At this point the index can be checked for the current element.

        }