Java中ListIterator中的哪个元素以前被认为是? [英] Java which element in the ListIterator is considered previous?

查看:190
本文介绍了Java中ListIterator中的哪个元素以前被认为是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试学习如何实现自己的ListIterators。除了我对previous()方法感到困惑之外,我已经实现了大部分并准备好了。按照标准惯例,我可以解释一下通常如何解释previous()。

I'm currently trying to learn how to implement my own ListIterators. I have most of it implemented and ready to go, except I'm confused by the previous() method. By standard convention, can I get an explanation of how previous() is usually interpreted.

ie:

             >cursor<
dog     cat    fish     bird     frog    snake

根据Oracles Java Platform 7 API:

According to Oracles Java Platform 7 API:

E previous()

E previous()

返回列表中的上一个元素并向后移动光标位置。可以重复调用此方法以向后遍历列表,或者与调用next()混合以来回传递。 (注意,对next和previous的交替调用将重复返回相同的元素。)

Returns the previous element in the list and moves the cursor position backwards. This method may be called repeatedly to iterate through the list backwards, or intermixed with calls to next() to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.)

我不太明白,如果调用previous(),它是否返回'fish'或'cat'。

What I don't quite understand is if previous() is called, does it return 'fish' or 'cat'.

我理解它有两种方式:

1)'fish'是你以前的对象

1) 'fish' was the object that you were previously at

2)'cat'是指数'fish'之前数字上的对象

2) 'cat' is the object at the index numerically previous to the index of 'fish'

如果之前返回'fish',那么remove()实际上是否删除相同的元素而不管遍历的方向?

If previous returns 'fish', then does remove() actually remove the same element regardless of the direction of traversal?

推荐答案

最简单的想法是将光标视为两个元素之间的。所以在开始时,它是之前的 dog 。调用 next()返回 dog ,并将其移至 dog cat 等等。 <$ em> snake 时已完成迭代>。

It's easiest to think of the cursor as being between two elements. So at the start, it's before dog. Calling next() returns dog, and moves it to between dog and cat, etc. You've finished iterating when the cursor is after snake.

所以 next()总是返回光标后的值,并将光标移动到后面的位置那。 previous()始终在光标前返回值,并将光标移动到之前的位置。

So next() always returns the value after the cursor, and moves the cursor to the position after that. previous() always returns the value before the cursor, and moves the cursor to the position before that.

编辑:正如David Conrad在评论中所述: remove()始终删除 next返回的最后一个值( ) previous()。这可能是最不明显的部分,当谈到我上面解释的方式时...使用你所描述的那种on item光标来模拟这种行为要容易得多。

As noted by David Conrad in comments: remove() always removes the last value that was returned by either next() or previous(). That's probably the least obvious part, when it comes to the way I've explained it above... it's much easier to model that behaviour using the sort of "on item" cursor you've described.

这篇关于Java中ListIterator中的哪个元素以前被认为是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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