CSC297.Java 2003F, Class 16: Lists, Continued Admin: * What is BishopD's latest assignment? (None) * What should your latest assignment be? (TBD) * Read the official Java list interface and note what methods you think should be added to our interface. (java.util.List and java.util.Collection are the important interfaces) * Finish VectorBasedList.java * Read the Vector documentation more closely. You can do something better for addToFront. * Have a great weekend! Overview: * Lists as mutable structures * List Wrappers * Other list strategies Lists as Mutable Structures * What Sam just said: The lists we've built so far are *immutable*: Once you've built one, it doesn't change. * You can build new lists based on it. * But the original list stays the same. * Many objects are immutable. * You don't change rational numbers. * You don't change strings * A few objects are mutable * Vectors change: When you add something to a vector, you don't get a new vector, but instead your vector has expanded. * How do you know? You write code that shows you (or you analyze the code) (or both) SchemeList sl = new ArrayBasedSchemeList(); MyVector mv = new MyVector(); // At this point, both things have no elements. out.println("Is sl empty? " + sl.isEmpty()); out.println("Is mv empty? " + (mv.length() == 0)); // Add something to each sl.addToFront("Hello"); mv.add("Hello"); // Check again out.println("Is sl empty? " + sl.isEmpty()); out.println("Is mv empty? " + (mv.length() == 0)); * When you design a new class, you must decide whether objects in that class are mutable. * It is perfectly reasonable to design a list in which the elements are mutable. /What can you do with a "mutable list"?/ * Create it. * All the stuff we did in the ImmutableList, but slightly different * addToFront * length * getFirstElement * deleteFirstElement * isEmpty * convert the string * Really cool stuff * reverse * make a new list based in "interesting" ways on the original list (e.g., each element of the new list is a permutation of the original list) * Moderately cool, but potentially reasonable * copy the list See MutableList.java for an interface that lists all of these things. Design decisions: * Should removeFirst return nothing or the element removed? * Sarcasm in code is good. Three possible directions: * Attempt to implement mutable lists * Using arrays or vectors * Using Cons cells * Look at the official "Java list" interface and compare * Yvonne decides to try implementing with vectors