码迷,mamicode.com
首页 > 编程语言 > 详细

Java 迭代器

时间:2014-07-18 10:21:10      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:style   blog   java   color   os   io   

1.删除

 1     public  static void main(String[] args) {
 2         HashSet hs=new HashSet();
 3         hs.add("mm");
 4         hs.add("kk");
 5         hs.add("ss");
 6         Iterator itor=hs.iterator();
 7 
 8         while(itor.hasNext()){
 9             String c=(String)itor.next();
10             if(c=="kk"){
11                 itor.remove();
12             }
13         }
14 
15        System.out.println(hs.toString());
16 
17     }

 

2.多种实现 

 1 class ReversibleArrayList<T> extends ArrayList<T> {
 2     public ReversibleArrayList(Collection<T> c) { 
 3         super(c);
 4     }
 5     
 6     public Iterable<T> reversed() {
 7              return new Iterable<T>() {
 8                       public Iterator<T> iterator() {
 9                             return new Iterator<T>() {
10                                   int current = size() - 1;
11                                   public boolean hasNext() { 
12                                       return current > -1; 
13                                   }
14                                   public T next() { 
15                                       return get(current--);
16                                   }
17                                   public void remove() { 
18                                         throw new UnsupportedOperationException();
19                                   }
20                             };
21                   }
22             };
23       }
24 }    
25 
26 public class AdapterMethodIdiom {
27     public static void main(String[] args) {
28         ReversibleArrayList<String> ral = new ReversibleArrayList<String>(Arrays.asList("To be or not to be".split(" ")));
29         for(String s : ral)
30           System.out.print(s + " ");
31         
32         System.out.println("");
33         
34         for(String s : ral.reversed())
35           System.out.print(s + " ");
36   }
37 } 

Java 迭代器,布布扣,bubuko.com

Java 迭代器

标签:style   blog   java   color   os   io   

原文地址:http://www.cnblogs.com/yuyutianxia/p/3852519.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!