Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法获取ListIterator的输出_Java_List - Fatal编程技术网

Java 无法获取ListIterator的输出

Java 无法获取ListIterator的输出,java,list,Java,List,我正在解决一个问题,我想使用ListIterator,而且,我已经在google上查看了ListIterator,但仍然没有得到输出。我的代码如下: public static void main (String[] args) { //code Scanner sc = new Scanner(System.in); int testCases = sc.nextInt(); for(int testIndex=0;testIndex<testCases;

我正在解决一个问题,我想使用ListIterator,而且,我已经在google上查看了ListIterator,但仍然没有得到输出。我的代码如下:

public static void main (String[] args) {
    //code
    Scanner sc = new Scanner(System.in);
    int testCases = sc.nextInt();
    for(int testIndex=0;testIndex<testCases;testIndex++)
    {
        int size = sc.nextInt();
        List<Integer> al = new ArrayList<>();
                int[] arrays = new int[size];
        for(int offset=0;offset<size;offset++)
        {
             al.add(sc.nextInt());
        }
                System.out.println(al);
                System.out.println();
        ListIterator lItr = al.listIterator();
                while(lItr.hasPrevious()){
                    System.out.print(lItr.previous()+" ");
                }
    }
}
publicstaticvoidmain(字符串[]args){
//代码
扫描仪sc=新的扫描仪(System.in);
int testCases=sc.nextInt();

对于(int testIndex=0;testIndex当您使用
listierator()
创建
listierator
时,迭代器被初始化为指向
列表的开头。因此,没有前面的元素

您可以使用
listIterator(int-index)
从任意位置开始迭代,将有一个上一个元素

要从
列表的末尾开始迭代,请使用:

ListIterator<Integer> lItr = al.listIterator(al.size());
ListIterator lItr=al.ListIterator(al.size());

ListIterator
从列表的开头开始,没有任何前面的元素。下面的问题与此完全相同。
请参阅。

是否尝试以相反的顺序迭代元素?所有迭代器都从开头开始。是的,我想以相反的顺序迭代列表。谢谢,它在联机编译器上工作,但在NetBeans ide上不工作。另外,请参阅此链接。listIterator中没有提到大小。根据您的说法,这是否正确@AsheeshSahu链接的示例从列表的开头开始(因此没有传递给方法的参数),向前迭代所有元素,然后向后迭代所有元素。您只能向后迭代,因此必须初始化迭代器以指向列表的末尾。可能在您的NetBeans尝试中列表为空。这意味着如果不想在listIterator中传递列表的大小,则首先必须迭代listIter前锋?