public interface GenericInterface <T> ...{ T next(); }
public class GenericTest3 implements GenericInterface<Integer> ...{ private int[] a; private int size; private int pos = 0; private int addPos = 0;
public GenericTest3(int sz) ...{ this.size = sz; a = new int[sz]; }
public Integer next() ...{ if ( pos > addPos - 1 ) ...{ pos = 0; }
return a[pos++]; }
public boolean hasNext() ...{ return !(pos == addPos); }
public boolean add(int value) ...{ if ( addPos >= size ) ...{ return false; } else ...{ a[addPos++] = value; return true; } }
public int getSize() ...{ return size; } }
public class GenericTest4 implements GenericInterface<String> ...{ private String[] a; private int size; private int pos = 0; private int addPos = 0;
public GenericTest4(int sz) ...{ this.size = sz; a = new String[sz]; }
public String next() ...{ if ( pos > addPos - 1 ) ...{ pos = 0; }
return a[pos++]; }
public boolean hasNext() ...{ return !(pos == addPos); }
public boolean add(String value) ...{ if ( addPos >= size ) ...{ return false; } else ...{ a[addPos++] = value; return true; } }
public int getSize() ...{ return size; } }
public class GenericTest5 ...{ public static void main(String[] args) ...{ GenericTest3 g3 = new GenericTest3(3); GenericTest4 g4 = new GenericTest4(4); String[] values = "a,b,c,d,e,f,g,h,i,j,k,l".splIT(",");
for (int i = 0; i < g3.getSize(); i++) ...{ g3.add(i); }
for (int i = 0; i < g4.getSize(); i++) ...{ g4.add(values[i]); }
while(g3.hasNext()) ...{ System.out.println(g3.next()); }
while(g4.hasNext()) ...{ System.out.println(g4.next()); } } } |