Java LinkedList ListIterator行为 [英] Java LinkedList ListIterator behavior

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

问题描述

我正在使用java.util.LinkedList上的java.util.ListIterator,希望它像下面的伪代码一样工作:

I was working with a java.util.ListIterator on a java.util.LinkedList expecting it to work like in this pseudocode:

list = (1,2,3,4)
iterator.next should be 1
iterator.next should be 2
iterator.prev should be 1
iterator.next should be 2

但是顺序是这样的:

iterator.next is 1
iterator.next is 2
iterator.prev is 2
iterator.next is 2

我不敢相信这是它的工作方式,所以我创建了一个测试,但是它产生了相同的输出. 因此,我仔细研究了ListIterator的定义,该定义当然是:

I couldn't believe that this is the way it works, so I created a test, but it produces the same output. So I had a closer look at the definition of ListIterator which of course is:

next()
Returns the next element in the list and advances the cursor position.
previous()
Returns the previous element in the list and moves the cursor position backwards.

所以实现是正确的,但是我仍然要问为什么他们选择了这种行为?是不是我的理解方式更直观?

So the implementation is correct, but I remain with the question why they have chosen this behavior? Wouldn't it be much more intuitiv the way i got it?

这是测试的代码:

import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import java.util.LinkedList;
import java.util.ListIterator;

public class LinkedListTest {
    ListIterator<Integer> iterator;

    @Before
    public void setUp() throws Exception {
        LinkedList<Integer> list = new LinkedList<>();
        for (int i = 1; i < 5; i++) {
            list.add(i);
        }
        iterator = list.listIterator();
    }

    @Test
    public void successfullTest() throws Exception
    {
        assertEquals(1, (int) iterator.next());
        assertEquals(2, (int) iterator.next());
        assertEquals(2, (int) iterator.previous());
        assertEquals(2, (int) iterator.next());
        assertEquals(3, (int) iterator.next());
        assertEquals(4, (int) iterator.next());
    }

    @Test
    public void failingTest() throws Exception
    {
        assertEquals(1, (int) iterator.next());
        assertEquals(2, (int) iterator.next());
        assertEquals(1, (int) iterator.previous());
        assertEquals(2, (int) iterator.next());
        assertEquals(3, (int) iterator.next());
        assertEquals(4, (int) iterator.next());
    }
}

推荐答案

可以想象Java中的迭代器从不指向特定元素,而是指向第一个元素之前,两个元素之间的中间或紧接在该元素之后.最后一个元素.

It is useful to imagine that iterators in Java never points to the specific element, but either before the first element, in the middle between two elements or just after the last element.

因此,当创建迭代器时,它看起来像

So, when iterator created, it looks like

 1 2 3 4
^

当您调用next时,将返回1并且迭代器将向前移动:

When you call next, 1 is returned and iterators moves forward:

 1 2 3 4
  ^    

再次调用next时,将返回2并且迭代器将向前移动:

When you call next again, 2 is returned and iterators moves forward:

 1 2 3 4
    ^

当您调用prev时,将返回2并且迭代器向后移动:

When you call prev, 2 is returned and iterators moves backward:

 1 2 3 4
  ^    

因此,对next的下一次调用将返回2.

So the next call to next will return 2.

请注意,无法获得迭代器的当前"值.获取值的唯一方法是移动迭代器.

Notice that there is no way to get "current" value of the iterator. The only way to get the value is to move the iterator.

我们可以在C ++中看到的另一种实现迭代器的方式.要使用C ++迭代器,我们需要三个单独的操作:检索当前值,检查是否存在要检索和移动迭代器的移动值.尽管Java方法仅需要执行两个操作:检查是否有要检索的移动值以及get-value-move-iterator.因此,在Java中实现自定义迭代器比在C ++中实现更简单.

The other way of implementing iterators we could see in C++. To use C++ iterator we need three separate actions: retrieve current value, check that there are move values to retrieve and move iterator. While java approach needs only two actions: check that there are move values to retrieve and get-value-and-move-iterator. So it is simpler to implement custom iterator in Java than in C++.

这篇关于Java LinkedList ListIterator行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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