使用 ListIterator 在 Java 中的 LinkedList 上来回移动 [英] Using ListIterator to move back and forth over a LinkedList in Java

查看:19
本文介绍了使用 ListIterator 在 Java 中的 LinkedList 上来回移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 LinkedList,我需要在它上面来回迭代多次.我使用它来跟踪将动态创建的工作流中的一系列页面.这不像我期望的那样表现.给定这个例子:

I have a LinkedList over which I need to iterate back and forth multiple times. I am using it to keep track of a series of pages in a workflow that will be created dynamically. This does not behave as I would expect. Given this example:

LinkedList<String> navigationCases;
navigationCases.add("page1");
navigationCases.add("page2");
navigationCases.add("page3");
navigationCases.add("page4");

ListIterator navigationItr = navigationCases.listIterator();
navigationItr.next(); // Returns page1
navigationItr.next(); // Returns page2
navigationItr.previous(); //Returns page2 again
navigationItr.next(); //Returns page2 again

我以为我可能是错误地构建了我的列表,或者错误地使用了迭代器,但在阅读了文档后,这似乎是故意的:

I thought perhaps I was building my list incorrectly, or using the Iterator wrong, but after reading the documentation, this seems to be by design:

ListIterator 没有当前元素;它的光标位置始终位于调用 previous() 返回的元素和调用 next() 返回的元素之间.

还有:

(Next) 返回列表中的下一个元素.该方法可能会被重复调用以遍历列表,或者与对前一个的调用混合来回.(请注意,交替调用 next 和 previous 将重复返回相同的元素.)

所以在阅读本文之后,很清楚为什么我的代码的行为方式如此.我只是不明白为什么它应该这样工作.甚至 remove 似乎也在向后弯曲以适应这种实现:

So after reading this, it is clear why my code is behaving the way it does. I just don't understand why it should work this way. Even remove seems to be bending over backwards to accommodate this implementation:

注意 remove() 和 set(Object) 方法不是根据光标位置定义的;它们被定义为对调用 next() 或 previous() 返回的最后一个元素进行操作.

从概念上讲,LinkedList 似乎很好地模拟了我的工作流案例,但我不能使用具有这种行为的迭代器.我在这里遗漏了什么,还是我应该编写自己的类来维护一个案例列表并浏览它们?

Conceptually, a LinkedList seemed to model my workflow cases pretty well, but I can't use an Iterator that behaves this way. Am I missing something here, or should I just write my own class maintain a list of cases and navigate through them?

推荐答案

这应该可以完成您的工作:

This should do your job:

public class Main {
    public static void main(String[] args) {
        final LinkedList<String> list = new LinkedList<String> ();

        list.add ("1"); list.add ("2"); list.add ("3"); list.add ("4");

        final MyIterator<String> it = new MyIterator (list.listIterator());

        System.out.println(it.next());
        System.out.println(it.next ());
        System.out.println(it.next ());
        System.out.println(it.previous ());
        System.out.println(it.previous ());
        System.out.println(it.next ());
    }

    public static class MyIterator<T> {

        private final ListIterator<T> listIterator;

        private boolean nextWasCalled = false;
        private boolean previousWasCalled = false;

        public MyIterator(ListIterator<T> listIterator) {
            this.listIterator = listIterator;
        }

        public T next() {
            nextWasCalled = true;
            if (previousWasCalled) {
                previousWasCalled = false;
                listIterator.next ();
            }
            return listIterator.next ();
        }

        public T previous() {
            if (nextWasCalled) {
                listIterator.previous();
                nextWasCalled = false;
            }
            previousWasCalled = true;
            return listIterator.previous();
        }

    }   
}

还有一个小提琴.

这篇关于使用 ListIterator 在 Java 中的 LinkedList 上来回移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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