Java ListIterator 以前的方法不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19631699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:58:04  来源:igfitidea点击:

ListIterator previous method not working

javaiteratorlistiterator

提问by tcp

package wrap;
import java.util.*;
public class ArrayListDemo {

    public static void main(String [] args){
        ArrayList<String> a=new ArrayList<String>();
        a.add("B");
        a.add("C");
        a.add("D");
        ListIterator<String> i=a.listIterator();
        while(i.hasPrevious()){
            System.out.println(i.previous());
        }
    }

}

The program works fine for hasNext() and next() methods but for hasPrevious() and previous() it displays a message as below::

该程序适用于 hasNext() 和 next() 方法,但对于 hasPrevious() 和 previous() 它显示如下消息:

<terminated> ArrayListDemo [Java Application] C:\Program Files (x86)\Java\jre7\bin\javaw.exe (28-Oct-2013 3:20:35 PM)

回答by Jeroen Vannevel

It will start at the front of the list and thus nothing is before that point. If you want to use these methods, use a ListIterator.

它将从列表的前面开始,因此在此之前没有任何内容。如果要使用这些方法,请使用ListIterator.

Docs.

文档

回答by Alexis C.

From the doc :

从文档:

public ListIterator<E> listIterator()

Returns a list iterator over the elements in this list (in proper sequence).

返回此列表中元素的列表迭代器(按适当的顺序)。

and

boolean hasPrevious()

Returns true if this list iterator has more elements when traversing the list in the reverse direction.

如果此列表迭代器在反向遍历列表时有更多元素,则返回 true。

Because the iterator is in the first position, hasPrevious()will return false and hence the while loop is not executed.

因为迭代器位于第一个位置,hasPrevious()将返回 false,因此不会执行 while 循环。

 a's elements

    "B"  "C"  "D"
     ^
     |

Iterator is in first position so there is no previous element


If you do :

如果你这样做:

    ListIterator<String> i=a.listIterator(); <- in first position
    i.next(); <- move the iterator to the second position
    while(i.hasPrevious()){
        System.out.println(i.previous());
    }

It will print "B"because you're in the following situation :

它将打印,"B"因为您处于以下情况:



a的元素

        "B"  "C"  "D"
              ^
              |
    Iterator is in second position so the previous element is "B"


You could also use the method listIterator(int index). It allows you to place the iterator at the position defined by index.

您也可以使用该方法listIterator(int index)。它允许您将迭代器放置在由 定义的位置index

If you do :

如果你这样做:

ListIterator<String> i=a.listIterator(a.size());

It will print

它会打印

D
C
B

回答by Prabhakaran Ramaswamy

ListIterator<String> i=a.listIterator();

initially iterator i will point to index of 0

最初的迭代器我会指向 index of 0

You are at index 0so there is no previous element.

你在index 0所以没有前一个元素。

回答by Aniket Thakur

You gave to reach to the end of the list or anywhere in between to traverse back. There is no previous element to element at index 0.

您可以到达列表的末尾或中间的任何位置以返回。索引 0 处的元素没有前一个元素。

Eg

例如

System.out.println("Elements in forward directiton");
        while(i.hasNext()){
            System.out.println(i.next());
        }
System.out.println("Elements in backward directiton");
        while(i.hasPrevious()){
            System.out.println(i.previous());
        }

回答by SudoRahul

Since you get the default ListIteratorfor the list, it starts with the first element, which is why hasPrevious()returns falseand the whileloop is exited. If you want to traverse the list in the reverse order, get the ListIteratorfrom the last index and traverse backwards using the hasPrevious()and previous()methods.

由于您获得ListIterator列表的默认值,它从第一个元素开始,这就是hasPrevious()返回falsewhile退出循环的原因。如果要以相反的顺序遍历列表,请ListIterator从最后一个索引获取 并使用hasPrevious()previous()方法向后遍历。

ListIterator<String> i = a.listIterator(a.size()); // Get the list iterator from the last index
while (i.hasPrevious()) {
    System.out.println(i.previous());
}

回答by Venu Sam

As said by others, You pointer is still pointing to first element in List so -1 is not possible.

正如其他人所说,你的指针仍然指向 List 中的第一个元素,所以 -1 是不可能的。

List of size n will have 1,2 ....n-1 as index.

大小为 n 的列表将 1,2 ....n-1 作为索引。

n-1 is not -1.

n-1 不是 -1。

THis is the reason why hasPrevious and previous are not working for you.

这就是 hasPrevious 和 previous 不适合您的原因。

Cheers.

干杯。