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

查看:158
本文介绍了使用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

我想也许我正在错误地构建列表,或者使用Iterator错误,但在阅读文档之后,这似乎是设计出来的:

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和previous的交替调用将重复返回相同的元素。)

因此,在阅读本文之后,很明显为什么我的代码表现为它的方式。我只是不明白为什么它应该这样工作。甚至删除似乎都是向后弯曲以适应这种实现:

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?

推荐答案

这个应该做你的工作:

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();
        }

    }   
}

并且a 小提琴

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

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