Exercises for Nested Classes

 

 

The answer is below

  1 public class DataStructure {
  2 
  3   private final static int SIZE = 15;
  4   private int[] arrayOfInt = new int[SIZE];
  5 
  6   DataStructure(){
  7     for (int i = 0; i < SIZE; i++)
  8       arrayOfInt[i] = i;
  9   }
 10 
 11   public int getSize(){
 12     return SIZE;
 13   }
 14 
 15   public int getInt(int index){
 16     return arrayOfInt[index];
 17   }
 18 
 19   // interface is default static
 20   interface DataStructureIterator extends Iterator<Integer>{}
 21 
 22   // non-static nested class(inner class), like an instance method
 23   private class EvenIterator implements DataStructureIterator{
 24 
 25     private int nextIndex = 0;
 26 
 27     @Override
 28     public boolean hasNext() {
 29       return nextIndex < SIZE;
 30     }
 31 
 32     @Override
 33     public Integer next() {
 34       Integer value = Integer.valueOf(arrayOfInt[nextIndex]);
 35       nextIndex += 2;
 36       return value;
 37     }
 38   }
 39 
 40   public DataStructureIterator getEventIterator(){
 41     return new EvenIterator();
 42   }
 43 
 44   public void printEven(){
 45     DataStructureIterator iterator = new EvenIterator();
 46     while (iterator.hasNext())
 47       System.out.print(iterator.next() + " ");
 48     System.out.println();
 49   }
 50 
 51   // more generic print method, through an interface type parameter
 52   public void print(DataStructureIterator iterator){
 53     while (iterator.hasNext())
 54       System.out.print(iterator.next() + " ");
 55     System.out.println();
 56   }
 57 
 58   // pass a functional interface type parameter, for lambda expression invoking
 59   public void print(Function<Integer, Boolean> function){
 60     for (int i = 0; i < SIZE; i++){
 61       if (function.apply(i)) // like a filter
 62         System.out.print(arrayOfInt[i] + " ");
 63     }
 64     System.out.println();
 65   }
 66 
 67   // as a method reference, for substitution of lambda expression
 68   public static Boolean isEvenIndex(Integer index){
 69     if (index % 2 == 0) return Boolean.TRUE;
 70     return Boolean.FALSE;
 71   }
 72 
 73   public static Boolean isOddIndex(Integer index){
 74     if (index % 2 == 0) return Boolean.FALSE;
 75     return Boolean.TRUE;
 76   }
 77 
 78   public static void main(String[] args) {
 79 
 80     DataStructure ds = new DataStructure();
 81 
 82     System.out.println("printEven()");
 83     ds.printEven();
 84 
 85     System.out.println("print(DataStructureIterator) with getEvenIterator()");
 86     ds.print(ds.getEventIterator());
 87 
 88     System.out.println("print(DataStructureIterator) with an instance of EventIterator");
 89     ds.print(ds.new EvenIterator());
 90 
 91     System.out.println("print(DataStructureIterator) with anonymous class, odd index");
 92     ds.print(new DataStructureIterator() {
 93       private int nextIndex = 1;
 94       public boolean hasNext() {
 95         return nextIndex < ds.getSize();
 96       }
 97       public Integer next() {
 98         Integer value = Integer.valueOf(ds.getInt(nextIndex));
 99         nextIndex += 2;
100         return value;
101       }
102     });
103 
104     System.out.println("print(Function) with lambda expression, even index");
105     ds.print(index -> index % 2 == 0 ? true : false);
106     ds.print(index -> index % 2 == 0 ? false : true );
107 
108     System.out.println("print(Function) with method references");
109     ds.print(DataStructure::isEvenIndex);
110     ds.print(DataStructure::isOddIndex);
111   }
112 }

 

reference from: https://docs.oracle.com/javase/tutorial/java/javaOO/QandE/nested-questions.html

posted @ 2020-08-12 12:51  deep-thinking  阅读(96)  评论(0编辑  收藏  举报