适配器方法惯用法

 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 }
原文地址:https://www.cnblogs.com/vector11248/p/7742573.html