java 如何以相反的顺序迭代 LinkedList 元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32546617/
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
How to iterate a LinkedList elements in reverse order?
提问by Vinoth Vino
I m new to Java Collections and my doubt is why can't i traverse a element in linkedlist in backward directions.Below I'll explain what i did and please clarify my doubts.
我是 Java 集合的新手,我的疑问是为什么我不能向后遍历链表中的元素。下面我将解释我做了什么,并请澄清我的疑虑。
- I've created interface iteratorfor forward iterations and listiteratorfor backward iterations. Why backward iterations are not working ?
Can't i use iteratorand listiteratorinterface in the same program to traverse a set of elements in forward and backward iterations ?
Code Snippet :
import java.util.*; class NewClass{ public static void main(String args[]){ LinkedList<String> obj = new LinkedList<String>(); obj.add("vino"); obj.add("ajith"); obj.add("praveen"); obj.add("naveen"); System.out.println(obj); System.out.println("For loop "); //using for loop for(int count=0; count < obj.size(); count++){ System.out.println(obj.get(count)); } System.out.println("For each loop "); //using foreach loop for(String s:obj){ System.out.println(s); } System.out.println("Whileloop "); //using whileloop int count=0; while(obj.size() > count){ System.out.println(obj.get(count)); count++; } System.out.println("Forward Iterations "); //using iterator Iterator it = obj.iterator(); while(it.hasNext()){ System.out.println(it.next()); } ListIterator lit = obj.listIterator(); System.out.println("Backward Iterations"); while(lit.hasPrevious()){ System.out.println(lit.previous()); } } }
Output
[vino, ajith, praveen, naveen] For loop vino ajith praveen naveen For each loop vino ajith praveen naveen Whileloop vino ajith praveen naveen Forward Iterations vino ajith praveen naveen Backward Iterations
- 我创建的接口迭代器的前进和迭代的ListIterator用于向后迭代。为什么向后迭代不起作用?
我不能在同一个程序中使用迭代器和列表迭代器接口来在向前和向后迭代中遍历一组元素吗?
代码片段:
import java.util.*; class NewClass{ public static void main(String args[]){ LinkedList<String> obj = new LinkedList<String>(); obj.add("vino"); obj.add("ajith"); obj.add("praveen"); obj.add("naveen"); System.out.println(obj); System.out.println("For loop "); //using for loop for(int count=0; count < obj.size(); count++){ System.out.println(obj.get(count)); } System.out.println("For each loop "); //using foreach loop for(String s:obj){ System.out.println(s); } System.out.println("Whileloop "); //using whileloop int count=0; while(obj.size() > count){ System.out.println(obj.get(count)); count++; } System.out.println("Forward Iterations "); //using iterator Iterator it = obj.iterator(); while(it.hasNext()){ System.out.println(it.next()); } ListIterator lit = obj.listIterator(); System.out.println("Backward Iterations"); while(lit.hasPrevious()){ System.out.println(lit.previous()); } } }
输出
[vino, ajith, praveen, naveen] For loop vino ajith praveen naveen For each loop vino ajith praveen naveen Whileloop vino ajith praveen naveen Forward Iterations vino ajith praveen naveen Backward Iterations
Where is the output for Backward Iterations? Please anyone help me.Thanks in advance
向后迭代的输出在哪里?请任何人帮助我。提前致谢
回答by tph
I think you want a descendingIterator
.
我想你想要一个descendingIterator
.
Iterator lit = obj.descendingIterator();
System.out.println("Backward Iterations");
while(lit.hasNext()){
System.out.println(lit.next());
}
回答by Paul Boddington
You can do it, but you need to use the method listIterator(int index)
to specify that you want to start at the end of the List
.
可以做,但是需要使用方法listIterator(int index)
指定要在List
.末尾开始。
LinkedList<String> obj = new LinkedList<String>();
obj.add("vino");
obj.add("ajith");
obj.add("praveen");
obj.add("naveen");
ListIterator<String> it = obj.listIterator(obj.size());
while (it.hasPrevious())
System.out.println(it.previous());