CS226 -- Day 12 -- Spring 2013 SLList.java - first task is to finish this generic singly linked list Read this tutorial on Generics and the Collections Framework in Java: http://docs.oracle.com/javase/tutorial/index.html Better Bag implementations: Iterators and Linked Lists INTRODUCING ITERATORS --------------------------------------------- Declaring Iterators w/the Java API ::::::::::::::::::::::: java.lang.Iterable interface - means the class has an iterator /** return initialized iterator for object */ Iterator iterator(); java.util.Iterator interface - means the class is an iterator - abstract process of scanning through collection - forward motion only - cursor is always before the next element - java interface methods: boolean hasNext(); T next(); void remove(); - eg: Scanner implements Iterator - bad to have multiple iterators modifying one list - java failsafe operation - conflicting remove throws ConcurrentModificationException Using Iterators::::::::::::::::::::::::::: foreach loop in Java - processes all items in array or iterable collection - on arrays: given initialized T[] ra for (T item: ra) // for i=0 to set for (T item : set) do something with item Implementing Iterators:::::::::::::::::::: Inner classes - declared in class as member - can directly access privates in outer class - use Outer.this to explicitly refer to outer class member (if conflict) * best way to implement an Iterator for a class Nested classes - declared in class, but static - can access privates in outer class if passed to nested class - we'll use these for nodes in linked lists EXAMPLES: Create Iterable version of Bag - make Bagi interface implement Iterable - make BagiArray implement Bagi - add Iterator inner class to BagiArray - main method shows some usage of the BagIterator - similarly we have BagiList, a singly linked list implementation of Bagi