Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.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 从LinkedList尾部到头部的遍历_Java_Iterator_Linked List_Traversal - Fatal编程技术网

Java 从LinkedList尾部到头部的遍历

Java 从LinkedList尾部到头部的遍历,java,iterator,linked-list,traversal,Java,Iterator,Linked List,Traversal,我需要遍历此自定义并显示其内容: 从头到尾 然后从头到尾 使用for循环,我可以非常轻松地从头到尾显示列表内容: for (AccountRecordSerializable account : list) { System.out.println(account); } 一切正常。现在我正试图扭转这种局面。在提供的要使用的LinkedList类中,该类中还包含LinkedListIterator类。迭代器类有一些方法,比如has

我需要遍历此自定义并显示其内容:

  • 从头到尾
  • 然后从头到尾

使用for循环,我可以非常轻松地从头到尾显示列表内容:

        for (AccountRecordSerializable account : list) {
            System.out.println(account); 
        }
一切正常。现在我正试图扭转这种局面。在提供的要使用的LinkedList类中,该类中还包含LinkedListIterator类。迭代器类有一些方法,比如
hasNext()
hasPrevious()
,我知道可以使用这些方法,但是我不太确定如何通过我的LinkedList使用迭代器来实现

有没有像我以前那样简单的方法来扭转这种局面?或者,我将如何使用迭代器类对列表进行迭代,以便它执行任务

如果这没有任何意义,我道歉。。。如果你需要澄清,请告诉我。谢谢

使用将实现您想要的功能:)

例如:

LinkedList<Integer> linkedList = new LinkedList<Integer>();
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);


Iterator<Integer> iterator = linkedList.descendingIterator();
while (iterator.hasNext())
{
    System.out.println(iterator.next());
}
LinkedList LinkedList=新建LinkedList();
添加(1);
添加(2);
linkedList.add(3);
迭代器迭代器=linkedList.DegendingIterator();
while(iterator.hasNext())
{
System.out.println(iterator.next());
}
如果要保存新的LinkedList,请反转。。只是

LinkedList<Integer> linkedList = new LinkedList<Integer>();
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);


Iterator<Integer> iterator = linkedList.descendingIterator();
LinkedList<Integer> reversed = new LinkedList<Integer>();
while (iterator.hasNext())
{
    reversed.add(iterator.next());
}
LinkedList LinkedList=新建LinkedList();
添加(1);
添加(2);
linkedList.add(3);
迭代器迭代器=linkedList.DegendingIterator();
LinkedList reversed=新建LinkedList();
while(iterator.hasNext())
{
add(iterator.next());
}
使用将实现您想要的功能:)

例如:

LinkedList<Integer> linkedList = new LinkedList<Integer>();
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);


Iterator<Integer> iterator = linkedList.descendingIterator();
while (iterator.hasNext())
{
    System.out.println(iterator.next());
}
LinkedList LinkedList=新建LinkedList();
添加(1);
添加(2);
linkedList.add(3);
迭代器迭代器=linkedList.DegendingIterator();
while(iterator.hasNext())
{
System.out.println(iterator.next());
}
如果要保存新的LinkedList,请反转。。只是

LinkedList<Integer> linkedList = new LinkedList<Integer>();
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);


Iterator<Integer> iterator = linkedList.descendingIterator();
LinkedList<Integer> reversed = new LinkedList<Integer>();
while (iterator.hasNext())
{
    reversed.add(iterator.next());
}
LinkedList LinkedList=新建LinkedList();
添加(1);
添加(2);
linkedList.add(3);
迭代器迭代器=linkedList.DegendingIterator();
LinkedList reversed=新建LinkedList();
while(iterator.hasNext())
{
add(iterator.next());
}
使用将实现您想要的功能:)

例如:

LinkedList<Integer> linkedList = new LinkedList<Integer>();
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);


Iterator<Integer> iterator = linkedList.descendingIterator();
while (iterator.hasNext())
{
    System.out.println(iterator.next());
}
LinkedList LinkedList=新建LinkedList();
添加(1);
添加(2);
linkedList.add(3);
迭代器迭代器=linkedList.DegendingIterator();
while(iterator.hasNext())
{
System.out.println(iterator.next());
}
如果要保存新的LinkedList,请反转。。只是

LinkedList<Integer> linkedList = new LinkedList<Integer>();
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);


Iterator<Integer> iterator = linkedList.descendingIterator();
LinkedList<Integer> reversed = new LinkedList<Integer>();
while (iterator.hasNext())
{
    reversed.add(iterator.next());
}
LinkedList LinkedList=新建LinkedList();
添加(1);
添加(2);
linkedList.add(3);
迭代器迭代器=linkedList.DegendingIterator();
LinkedList reversed=新建LinkedList();
while(iterator.hasNext())
{
add(iterator.next());
}
使用将实现您想要的功能:)

例如:

LinkedList<Integer> linkedList = new LinkedList<Integer>();
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);


Iterator<Integer> iterator = linkedList.descendingIterator();
while (iterator.hasNext())
{
    System.out.println(iterator.next());
}
LinkedList LinkedList=新建LinkedList();
添加(1);
添加(2);
linkedList.add(3);
迭代器迭代器=linkedList.DegendingIterator();
while(iterator.hasNext())
{
System.out.println(iterator.next());
}
如果要保存新的LinkedList,请反转。。只是

LinkedList<Integer> linkedList = new LinkedList<Integer>();
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);


Iterator<Integer> iterator = linkedList.descendingIterator();
LinkedList<Integer> reversed = new LinkedList<Integer>();
while (iterator.hasNext())
{
    reversed.add(iterator.next());
}
LinkedList LinkedList=新建LinkedList();
添加(1);
添加(2);
linkedList.add(3);
迭代器迭代器=linkedList.DegendingIterator();
LinkedList reversed=新建LinkedList();
while(iterator.hasNext())
{
add(iterator.next());
}

单链表并不意味着从尾部到头部进行遍历。您有两种选择

  • 反转链接列表并从头到尾遍历(对于原始链接列表,这将是从头到尾的遍历)
  • 有一堆。遍历链表并将元素放入堆栈中。然后继续从堆栈中弹出元素并打印

  • 单链表并不意味着从尾部到头部的遍历。您有两种选择

  • 反转链接列表并从头到尾遍历(对于原始链接列表,这将是从头到尾的遍历)
  • 有一堆。遍历链表并将元素放入堆栈中。然后继续从堆栈中弹出元素并打印

  • 单链表并不意味着从尾部到头部的遍历。您有两种选择

  • 反转链接列表并从头到尾遍历(对于原始链接列表,这将是从头到尾的遍历)
  • 有一堆。遍历链表并将元素放入堆栈中。然后继续从堆栈中弹出元素并打印

  • 单链表并不意味着从尾部到头部的遍历。您有两种选择

  • 反转链接列表并从头到尾遍历(对于原始链接列表,这将是从头到尾的遍历)
  • 有一堆。遍历链表并将元素放入堆栈中。然后继续从堆栈中弹出元素并打印

  • JavaLinkedList实现了提供方法的接口

    按相反顺序返回此数据块中元素的迭代器。元素将按从最后一个(尾部)到第一个(头部)的顺序返回

    我的建议是在类中实现该接口,并获得反向迭代器

    该数据结构具有一些属性,您应该使用这些属性来获取实现。链表的典型结构是一个元素指向下一个元素。在您的例子中,您有支持双链接列表的实现

    private int size = 0; // size can never be < 0
    private DLNode<E> head;
    private DLNode<E> tail;
    
    所以要打印你的元素,你可以这样做

     public <T> void printLinkedListFromHead(LinkedList<T> list) {
    
       for(ListIterator<T> iterator = list.listIterator(0); iterator.hasNext();) {
           System.out.println(iterator.next());
       }
    
     }
    

    将循环的迭代器升级范围缩小到while。

    Java LinkedList实现了提供方法的接口

    按相反顺序返回此数据块中元素的迭代器。元素将按从最后一个(尾部)到第一个(头部)的顺序返回

    我的建议是在类中实现该接口,并获得反向迭代器

    该数据结构具有一些特性