简体   繁体   English

java:重置ListIterator?

[英]java: resetting ListIterator?

I need to traverse a LinkedList a number of times, in a way that suggests using ListIterator . 我需要多次遍历LinkedList ,建议使用ListIterator

Is there a way to reset a ListIterator ? 有没有办法重置ListIterator or is it better to just create a new one? 还是创建一个新的更好? (and what if I can't because I don't have access to the list?) (如果我不能,因为我无权访问该列表,该怎么办?)

edit: and is there a way to create a ListIterator that points to the end of the list? 编辑:有没有办法创建一个指向列表末尾的ListIterator (so that hasNext() is false but I can use previous() or hasPrevious() ) (因此hasNext()为false但我可以使用previous()hasPrevious()

When it comes to performance, it's probably faster to create a new iterator. 在性能方面,创建新的迭代器可能会更快。 If you don't have the list, you can still use hasPrevious() and previous() to move backwards until you've placed the iterator at the beginning of the list. 如果你没有列表,你仍然可以使用hasPrevious()和previous()向后移动,直到你将迭代器放在列表的开头。 Depending on the list implementation, you may experience a relevant performance impact navigating backwards through the iterator. 根据列表实现,您可能会遇到通过迭代器向后导航的相关性能影响。

Looks like AbstractList.listIterator(int initialPos) is what I want to use for an ArrayList, and LinkedList.descendingIterator() is what I want to use for a LinkedList, but there doesn't appear to be a single method that would apply efficiently to both, and descendingIterator() returns an Iterator, not a ListIterator. 看起来像AbstractList.listIterator(int initialPos)是我想要用于ArrayList的东西, LinkedList.descendingIterator()是我想用于LinkedList的东西,但似乎没有一个方法可以有效地应用到两者,和descendingIterator()返回一个迭代器,而不是一个ListIterator。 drat. DRAT。

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class ListIteratorTest {
    static public void populate(List<Integer> list)
    {
        for (int i = 0; i < 10; ++i)
        {
            list.add(i*i);
        }       
    }
    static public void main(String[] args)
    {
        AbstractList<Integer> list = new ArrayList<Integer>();
        populate(list);

        ListIterator<Integer> it;       
        System.out.println("List going forwards:");
        it = list.listIterator();
        while (it.hasNext())
            System.out.println(it.next());

        System.out.println("List going backwards:");
        it = list.listIterator(list.size());
        while (it.hasPrevious())
            System.out.println(it.previous());

        LinkedList<Integer> list2 = new LinkedList<Integer>();
        populate(list2);
        System.out.println("List going forwards:");
        it = list2.listIterator();
        while (it.hasNext())
            System.out.println(it.next());

        System.out.println("List going backwards:");
        Iterator<Integer> it2 = list2.descendingIterator();
        while (it2.hasNext())
            System.out.println(it2.next());

    }
}

Create a new LinkedList based on the obtained ListIterator , so that you can get as many iterators from it as you want. 基于获得的ListIterator创建一个新的LinkedList ,以便您可以根据需要从中获取尽可能多的迭代器。

Edit : as to your second question which you edited in afterwards, consider doing a Collections#reverse() on the list first. 编辑 :关于您之后编辑的第二个问题,请考虑先在列表中执行Collections#reverse()

If you can just create new one that is probably your best bet. 如果你可以创造一个可能是你最好的选择。

If you cannot, as you are going through the list iterator, add each element to a new list. 如果您不能通过列表迭代器,则将每个元素添加到新列表中。 Use that new list to create the listIterator next time you need it. 使用该新列表可在下次需要时创建listIterator。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM