/** * A collection built on an array and returning a MyIterator subclass. * * @author R. P. Futrelle * @version 20 June 2003 (started 19 June 2003) */ class MyArrayCollection { Object[] storesIt; int maxIndexFilled; int _capacity; // Test this class using main(), w/o iterator public static void main(String[] args){ MyArrayCollection tester = new MyArrayCollection(3); tester.add("string1"); tester.add("string2"); tester.add("string3"); tester.add("string4"); tester.add("string5"); System.out.println(); for(int i = 0; i <= tester.maxIndexFilled; i++) System.out.println(tester.storesIt[i]); tester.clear(); tester.add("string10"); tester.add("string11"); System.out.println(); for(int i = 0; i <= tester.maxIndexFilled; i++) System.out.println(tester.storesIt[i]); /* results: string1 string2 string3 string10 string11 */ } // main() MyArrayCollection(int capacity) { _capacity = capacity; storesIt = new Object[_capacity]; init(); } void init() { maxIndexFilled = -1; } // init() MyIterator iterator() { return new MyArrayIterator(this); } boolean add(Object obj) { if(maxIndexFilled >= _capacity - 1) return false; storesIt[++maxIndexFilled] = obj; return true; } void clear() { init(); } } // MyArrayCollection