首页 > 其他 > 详细

适配器方法惯用法

时间:2017-10-27 15:02:13      阅读:249      评论:0      收藏:0      [点我收藏+]
 1 import java.util.*;
 2 class ReversibleArrayList<T> extends ArrayList<T>{
 3     public ReversibleArrayList(Collection<T> c) {super(c);}
 4     public Iterable<T> reversed(){
 5         return new Iterable<T>(){
 6 
 7             @Override
 8             public Iterator<T> iterator() {
 9                 return new Iterator<T>(){
10                     int current = size()-1;
11                     @Override
12                     public boolean hasNext() {
13                         return current > -1;
14                     }
15                     @Override
16                     public T next() {
17                         return get(current--);
18                     }
19                 };
20             }
21         };
22     }
23 }
24 
25 
26 public class AdapterMethodiom 
27 {
28     
29     public static void main(String[] args) {
30         ReversibleArrayList<String> ral = new ReversibleArrayList(Arrays.asList(("hello world ni hao").split(" ")));
31         for(String s:ral)
32         {
33             System.out.print(s+" ");
34         }
35         System.out.println();
36         for(String s:ral.reversed())
37         {
38             System.out.print(s+" ");
39         }
40         System.out.println();
41     }
42 }

 

适配器方法惯用法

原文:http://www.cnblogs.com/vector11248/p/7742573.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!