/**
 * @author mitsunori ogihara
 *
 */
public class FooList<E> {

	private Object[] array = new Object[10];
	private int capacity = 10;
	private int size = 0;
	
	FooList() {
		
	}
	/**
	 * adding at the end
	 * @param data	the data to be added
	 */
	public void add(E data) {
		// if capacity is reached, double the size
		if (size==capacity) doubleSize();
		array[size] = data;
		size++;
	}
	/**
	 * removable
	 * @param i	the location
	 */
	public void remove(int i) {
		if (i>=0 && i<size) {
			for (int j=i; j<size; j++) {
				array[j-1] = array[j];
			}
		}
	}
	/**
	 * find an element
	 * @param o	the element to be found
	 * @return	the position, -1 if non-existing element
	 */
	public int locate(E o) {
		for (int i=0; i<size; i++) {
			if (array[i].equals(o)) return i;
		}
		return -1;
	}
	/**
	 * change the value at a position
	 * @param i	the position
	 * @param data	the new data
	 */
	public void set(int i, E data) {
		if (i>=0 && i<size) {
			array[i] = data;
		}		
	}

	/**
	 * Obtaining an object at a given position
	 * @param index	the index
	 * @return	the object
	 */
	public E get(int index) {
		// use up-casting
		return (E)array[index];
	}
	
	/**
	 * double the cqpacity
	 */
	public void doubleSize() {
		Object[] newArray = new Object[capacity*2];
		for (int i=0; i<capacity; i++) {
			newArray[i] = array[i];
		}
		capacity *= 2;
		array = newArray;
	}
	/**
	 * printing method
	 */
	public void print() {
		for (int i=0; i<this.size; i++) {
			System.out.println("i = "+i+": "+array[i]);
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		FooList<Integer> bar = new FooList<Integer>();
		for (int i=0; i<10; i++) {
			bar.add(i);
		}
		bar.add(1100);
		bar.remove(10);
		bar.remove(9);
		bar.remove(8);
		System.out.println("1100 is at position "+bar.locate(1000));
		System.out.println("0 is at position "+bar.locate(0));
		bar.print();
	}

}
