import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;


public class ArrayListExample {
	public static void main(String[] args) {
		ArrayList<String> list = new ArrayList<String>(10);
		
		try {
			System.out.println("Initial array length: " + list.size());
			BufferedReader in = new BufferedReader(new FileReader("songlist.txt"));
			String next = null;
			while ((next = in.readLine()) != null) {
				int artistEndIndex = next.indexOf('-') + 1;
				list.add(next.substring(artistEndIndex).trim());
			}
			System.out.println("Final number of elements: " + list.size());
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		System.out.println("Element 42: " + list.get(42));
		list.remove(42);
		System.out.println("Element 42 is now: " + list.get(42));
		// Probably shouldn't be in classic rock archive, remove it.
		list.remove("Fiona Apple");
		System.out.println("Element 42 is now: " + list.get(42));
		// Remove only removes first entry, see if there are any more.
		int indexOfFiona = list.indexOf("Fiona Apple");
		if (indexOfFiona != -1) {
			System.out.println("Fiona found");
		} else {
			System.out.println("Fiona not found");
		}
		// Insert Fiona back into list.
		list.add(42, "Fiona Apple");
		// Change entry to something else.
		list.set(42, "Somebody else");
		
		/* Works, but not recommended.
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i);
		}
		*/
		
		Iterator<String> iter = list.iterator();
		while (iter.hasNext()) {
			System.out.println(iter.next());
		}
		
		// Remove all elements from list.
		list.clear();
		System.out.println("List is empty? " + list.isEmpty());
	}
}

