1 package testBlog; 2 3 import java.util.ArrayList; 4 import java.util.Collection; 5 import java.util.HashSet; 6 import java.util.Iterator; 7 import java.util.List; 8 import java.util.ListIterator; 9 import java.util.Set; 10 import java.util.TreeSet; 11 12 public class Test { 13 public static void main(String[] args) { 14 List<String> all = new ArrayList<>(); 15 all.add("abcdef"); 16 all.add("abcde"); 17 all.add("abcd"); 18 all.add("abc"); 19 20 ListIterator<String> iter = all.listIterator();//双向迭代器 21 22 System.out.println("由前向后输出"); 23 while(iter.hasNext()) { 24 String str = iter.next(); 25 System.out.println(str+","); 26 } 27 28 System.out.println("由后向前输出"); 29 while(iter.hasPrevious()) { 30 String str = iter.previous(); 31 System.out.println(str+","); 32 } 33 34 } 35 }
结果:
由前向后输出
abcdef,
abcde,
abcd,
abc,
由后向前输出
abc,
abcd,
abcde,
abcdef,