ListIterator实现逆序输出时没反应?

捏捏扭扭 2014-08-11 04:13:39
为什么没有输出呢?

import java.util.LinkedList;
import java.util.ListIterator;


public class Test
{
public static void main(String[] args)
{
LinkedList<String> colors1=new LinkedList<String>();

colors1.add("red");
colors1.add("blue");
colors1.add("yellow");
colors1.add("orange");

System.out.println("The reverse order:");

ListIterator<String> it=colors1.listIterator();
while(it.hasPrevious())
{
System.out.println(it.previous());
}

}
}
...全文
933 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
-阿克蒙德- 2014-08-12
  • 打赏
  • 举报
回复
引用 7 楼 u012942606 的回复:
[quote=引用 5 楼 u010863631 的回复:] 很容易实现吧。先遍历到末尾,然后在用hasPrevious()就行了,已验证。代码:
import java.util.LinkedList;
import java.util.ListIterator;

public class Test {
	public static void main(String[] args) {
		LinkedList<String> colors1 = new LinkedList<String>();
		colors1.add("red");
		colors1.add("blue");
		colors1.add("yellow");
		colors1.add("orange");

		System.out.println("The reverse order:");

		ListIterator<String> it = colors1.listIterator();
		while (it.hasNext()) {
			it.next();
		}
		while (it.hasPrevious()) {
			System.out.println(it.previous());
		}
	}
}
好了,我又重新写了下,实现了,谢谢你了,实现了。[/quote] 哈,已经解决了,我昨天还在思考怎么让“指针”先跳到最后一位,原来是这样弄得,涨姿势
捏捏扭扭 2014-08-11
  • 打赏
  • 举报
回复
引用 5 楼 u010863631 的回复:
很容易实现吧。先遍历到末尾,然后在用hasPrevious()就行了,已验证。代码:
import java.util.LinkedList;
import java.util.ListIterator;

public class Test {
	public static void main(String[] args) {
		LinkedList<String> colors1 = new LinkedList<String>();
		colors1.add("red");
		colors1.add("blue");
		colors1.add("yellow");
		colors1.add("orange");

		System.out.println("The reverse order:");

		ListIterator<String> it = colors1.listIterator();
		while (it.hasNext()) {
			it.next();
		}
		while (it.hasPrevious()) {
			System.out.println(it.previous());
		}
	}
}
好了,我又重新写了下,实现了,谢谢你了,实现了。
捏捏扭扭 2014-08-11
  • 打赏
  • 举报
回复
引用 5 楼 u010863631 的回复:
很容易实现吧。先遍历到末尾,然后在用hasPrevious()就行了,已验证。代码:
import java.util.LinkedList;
import java.util.ListIterator;

public class Test {
public static void main(String[] args) {
LinkedList<String> colors1 = new LinkedList<String>();
colors1.add("red");
colors1.add("blue");
colors1.add("yellow");
colors1.add("orange");

System.out.println("The reverse order:");

ListIterator<String> it = colors1.listIterator();
while (it.hasNext()) {
it.next();
}
while (it.hasPrevious()) {
System.out.println(it.previous());
}
}
}

谢谢你,这样确实可以运行出来。原谅我没有把题目说具体:
题目要求是:先用Iterator迭代器方法把所有的正序输出出来,然后再用ListIterator把所有的逆序输出出来,结果我的运行结果时ListIterator的迭代器没有发生作用;不知道为什么?
下面是我的代码和运行截图:
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

public class Test {
public static void main(String[] args) {
LinkedList<String> colors1 = new LinkedList<String>();
colors1.add("red");
colors1.add("blue");
colors1.add("yellow");
colors1.add("orange");

System.out.println("The colors are:");
Iterator it=colors1.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}

System.out.println("The reverse order:");
ListIterator<String> lit = colors1.listIterator();
while (lit.hasPrevious())
{
System.out.println(lit.previous());
}
}
}
「已注销」 2014-08-11
  • 打赏
  • 举报
回复
很容易实现吧。先遍历到末尾,然后在用hasPrevious()就行了,已验证。代码:
import java.util.LinkedList;
import java.util.ListIterator;

public class Test {
	public static void main(String[] args) {
		LinkedList<String> colors1 = new LinkedList<String>();
		colors1.add("red");
		colors1.add("blue");
		colors1.add("yellow");
		colors1.add("orange");

		System.out.println("The reverse order:");

		ListIterator<String> it = colors1.listIterator();
		while (it.hasNext()) {
			it.next();
		}
		while (it.hasPrevious()) {
			System.out.println(it.previous());
		}
	}
}
捏捏扭扭 2014-08-11
  • 打赏
  • 举报
回复
引用 3 楼 u012047741 的回复:
你想要倒序,,Collections.reverse(list)这样就可以了啊
题目要求是让用ListIterator实现逆序输出。。。。
-阿克蒙德- 2014-08-11
  • 打赏
  • 举报
回复
你想要倒序,,Collections.reverse(list)这样就可以了啊
捏捏扭扭 2014-08-11
  • 打赏
  • 举报
回复
引用 1 楼 vnvlyp 的回复:
ListIterator是从第一个开始的,你一上来就问hasPrevious那当然没有。
那应该怎么改呢?
vnvlyp 2014-08-11
  • 打赏
  • 举报
回复
ListIterator是从第一个开始的,你一上来就问hasPrevious那当然没有。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧