CSC297.Java, Class 17: Even More Lists! Admin: * We are not doing BishopD's new assignment. * What did you learn from the Java list class? * How did your homework go? * New homework: Read about the Java collections framework at http://java.sun.com/j2se/1.4.2/docs/api/ * New homework: Try implementing lists with vectors and the "store it backwards" strategy. We *will* look at your code on Wednesday. * Announcement: Two talks * Thursday, noon, 2413, Shaka McGlotten '97, "Gender, Sexuality, and other Stuff in Online Communities" * Friday, noon, 2413, Joshua Shaw Vickery '02, "Real-World Java Programming" Overview: * java.util.List * Vector-based-lists, revisited. * Extending those lists. * Implementing mutable lists with nodes. * Variations. /java.util.List/ * What methods are in java.util.List that are not in MutableList? * contains(Object) * list-ref * index-of * last-index-of * remove * sublist * many more * Which of these would we want to include? * The ones listed explicitly above /Vector-based Lists/ * Why would you ever want removeFirst to return something? try { while (obj = stuff.removeFirst()) { // Process obj ... } // while } // try catch (Exception e) { // Done removing and processing all the elements } // catch * Morals: * Many things you want to do are done in the API. * There are still some advantages to figuring out how to do things by hand. * Be careful about dealing with the length of a vector. * Questions: * About how many steps does it take to add something to the front of a Scheme list of length 100? One step: Cons the new thing to the list. * About how many steps does it take to add something to the back of a Scheme list of length 100? Approximately 200: 100 to cdr through the list until we hit the end, 100 to build up the new list of 101 elements. * About how many steps does it take to add something to the front of a VectorBasedList of length 100? About 100. * About how many steps does it take to add something to the back of a VectorBasedList of length 100? Sorry, this operation is not supported. * About how many steps does it take to delete the first element of a Scheme list of length 100? One step. * About how many steps does it take to delete the first element of a Vector-based list of length 100? 100 steps. * What does this suggest about our implementation? It sucks. /Improving VectorBasedLists/ * Suppose we only have the few operations we started with (particularly addToFront and deleteFirst as the only mutators). * Can we make our VectorBasedLists more efficient? * Try funky things with "wrapping around". [JLW] = [JWells] * Store the list in reverse order. /Improving Lists in General/ * What if we want to efficiently add to and remove from both ends of the list? * Reflection: What did we do last week to make the ImmutableLists more efficient? * Don't use Vectors or arrays. Instead, use Pairs/Cons Cells * Idea: We keep track of the cons cell of the front of the list and the cons cell of the back of the list. * To add to the front, shove another cons cell on and update our knowledge. * To add to the back, shove another cons cell on and upate our knowledge.