import java.util.ArrayList;

public class ArrayListWithCounts<E> extends ArrayList<E> {
    private ArrayList<Integer> counts;
    
    public ArrayListWithCounts() {
    	this.counts = new ArrayList<Integer>();
    }
    
    public boolean add(E value) {
    	int index = this.indexOf(value);
    	if (index >= 0) {
    		this.counts.set(index, this.counts.get(index)+1);
    	}
    	else {
    		super.add(value);
    		this.counts.add(1);
    	}
    	return true;
    }
    
    public int getCount(E value) {
    	int index = this.indexOf(value);
    	if (index >= 0) {
    		return this.counts.get(index);
    	}
    	else {
    		return 0;
    	}    	
    }
    
    public static void main(String[] args) {
    	ArrayListWithCounts<String> words = new ArrayListWithCounts<String>();
    	words.add("foo");
    	words.add("bar");
    	words.add("foo");
    	words.add("biz");
    	words.add("bar");
    	
    	for (String str : words) {
    		System.out.println(str + " " + words.getCount(str));
    	}
    	
    }
}
