CSC297.Java, Class 20: Aagh! Will we ever finish lists? (No) Admin * How did your homework go? * Yvonne says it wasn't that difficult * What is BishopD's new homework? (Or is there one?) No new homework * Why am I spending so much time on lists? * Challenge for Friday: Once we have iterators, what other methods do we want to add to iterators or lists? * E.g., to change the list Overview * Generalizing lists and vectors: Collections * Basic collection operations * What makes lists special collections? * Inheritance Morals from looking at your homework * Don't just copy and paste: Think about implication * In doubly-linked lists, removeLast should be a quick operation * In doubly-linked lists, there are lots and lots of references to update * Adding to back: Update the prev of the new cell and the next of the old back * Removing from back: Update the next of the new back to null * ... Why Spend Lots of Time on Lists? * An interesting example for thinking about efficiency * Useful data structures * They're important for some reason * Good example for thinking about building dynamic data structures * Good example for thinking about other important 152 issues, too How are lists and vectors similar? How are they different? * Similarities: * One primary purpose: Store lots of stuff * Differences: * Different internal representation. Vectors use a continuous chunk of memory, linked lists tend to use scattered cells. * Vectors provide faster and easier access to individual elements (by number) * Usually faster to change the length of a list, particularly to add to or delete from the front. We might generalize these things and come up with a new data type First new data type: The collection * Purpose: Store stuff Specializations: * Provide an option for referencing stuff sequentially * Provide an option for referencing things by number * Determine membership * Add stuff to the collection * Delete stuff from the collection * Extract a portion of the collection Observations: * No one has designed a data structure that implements all of these special operations efficiently * Different applications use collections in different ways * We therefore design variants of collections that provide efficient implementation of the operations a particular program needs most Lists are collections that * Permit quick adding and deletion (at least at the ends) * Permit quick "first element" and "last element" operations. * Lists are collections that you can easily step through from first to last * "Stepping through a collection" is normally referred to as "iteration" How do we provide access to iteration? * Scheme model: map * Alternate: front, current, advance l.front(); out.println(l.current()); for (i = 1; i < l.length; i++) { l.advance(); out.println(l.current()); } * Alternate: next() combines current and advance out.println(l.front()); for (i = 1; i < l.length; i++) { out.println(l.next()); } In the last two models, there is an interesting hidden issue: You can't iterate the list more than once simultaneously * Historical evidence shows that programmers often want to iterate lists simultaneously * Hence, well-designed lists provide separate iterators Iterator it = l.iterator(); out.println(it.current()); for (int i = 1; i < l.lenght; i++) { it.advance(); out.println(it.current()); }