今天碰到了个问题,折腾了我老半天,倒叙迭代时,一定要记得先把光标移至最后,再previous()
public static void main(String[] args){
Scanner scan ;
List<String> list = new ArrayList<String>();
while(true){
scan=new Scanner(System.in);
String str=scan.nextLine();
if("end".equals(str)){
break;
}
list.add(str);
}
System.out.println(list);
printStr(list);
scan.close();
}
private static void printStr(List<String> list) {
ListIterator<String> li = list.listIterator();
StringBuffer sbuf= new StringBuffer();
//光标必须要先移到最后
while(li.hasNext()){
li.next();
}
System.out.println("倒叙打印输出:");
while(li.hasPrevious()){
sbuf.append(li.previous());
}
System.out.println(sbuf.toString());
}
|