import java.util.Iterator; public class Main { public static void main(String[] args) { List list = new List(); System.out.println(list.isEmpty()); list.add("milk", 0); list.add("eggs", 0); list.add("butter", 0); list.add("apples", 0); list.add("bread", 0); list.add("chicken", 0); System.out.println(list.isEmpty()); for (String s : list) { System.out.println(s); } list.remove(list.length() / 2); for (String s : list) { System.out.println(s); } } } class List implements Iterable { public void add(E data, int index) { if (index < 0 || index > top) { throw new IndexOutOfBoundsException(); } else if (top >= list.length) { grow(); } for (int i = top; i > index; --i) { list[i] = list[i - 1]; } list[index] = data; ++top; } public void add(E data) { add(data, top); } public E get(int index) { if (index < 0 || index >= top) { throw new IndexOutOfBoundsException(); } return (E)list[index]; } private void grow() { Object[] temp = new Object[list.length * 2]; for (int i = 0; i < list.length; ++i) { temp[i] = list[i]; } list = temp; } public boolean isEmpty() { return top == 0; } public Iterator iterator() { return new Iterator() { public boolean hasNext() { return curr < top; } public E next() { return (E)list[curr++]; } public void remove() { throw new UnsupportedOperationException(); } private int curr; }; } public int length() { return top; } public E remove(int index) { E temp; if (index < 0 || index >= top) { throw new IndexOutOfBoundsException(); } temp = (E)list[index]; for (int i = index; i < top; ++i) { list[i] = list[i + 1]; } --top; return temp; } public void removeAll() { list = new Object[5]; } public boolean search(E key) { for (int i = 0; i < top; ++i) { if (key == list[i]) { return true; } } return false; } private Object[] list = new Object[5]; private int top; }