11/13/2006 CMSC 131 Fall 2006Rance Cleaveland©2006 Univeristy of MarylandLecture 32:IteratorsLast time:1. Collections2. Stack<T>3. ArrayList<T>Today1. Midterm 11/152. Iterators3. ReviewCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland1Midterm Exam: Wed. 11/15 Test will be given in discussion section Go to your own section! Test will be: Closed notes / book / neighbor / etc. Cover all material since beginning of course, with special emphasis on topics since last midterm (through exceptions) Study! Review notes, projects, quizzes Use study questions on web-siteCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland2Iterators Special objects for iterating through elements in collection Created using iterator() method of collectionArrayList<String> a = …;Iterator<String> i = a.iterator(); Iterator objects implement Iterator<T> interface Methods of interest <T> next() Return “current” element, advance boolean hasNext() Is there a next element? void remove() Remove current element Be sure to import java.util.Interface! In array paradigm: next() equivalent to a[i++]; hasNext() equivalent to i < a.lengthCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland3Examplepublic static String concat (ArrayList<String> a){Iterator<String> i = a.iterator();String conc = “”;while (i.hasNext()) {conc += i.next();}return conc;} Concatenates elements in an ArrayList Note use of iterator!CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland4Iterator Behavior When iterator is created, its “marker” starts at initial element of collection An iterator only moves forward No backing up No resetting To cycle through data again, create another iterator See IteratorExample.javaCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland5Iterators and Mutability If you are iterating through a collection and someone adds or removes an element, your iteratoris no longer valid!!! Usually a ConcurrentModification exception is thrown Calling the iterator's own remove() method does not ruin the iterator More than one iterator possible for the same collection simultaneously Do not use remove in this case! Why? Because of ConcurrentModification problem with other iteratorCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland6Mutable Strings Strings are immutable Once a String object is created, it cannot be altered For String objects, shallow = half-deep = deep copying (why?) Sometime mutable strings would be handy Sometimes a small change needs to be made to a string (e.g. misspelled name) Don’t want to create a whole new String object in this case StringBuffer: Java’s class for mutable StringsCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland7StringBuffer Basics See documentation at: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuffer.html Main methods append: add characters to end insert: add characters in middle delete: remove characters Note append, insert return object of type StringBuffer This is alias to object that the methods belong to!CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland8ExampleWhat is output of:public class StringBufferExample {public static void main (String[] args) {StringBuffer s = new StringBuffer ("foo");s.append ("BAR");System.out.println ("s: " + s);StringBuffer t = s.append (true);System.out.println ("s: " + s);System.out.println ("t: " + t);t.append ('b');System.out.println ("s: " + s);System.out.println ("t: " + t);s.delete(1,3);System.out.println ("s: " + s);}}CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland9Midterm ReviewWhat Is / Are …JUnit? mutable objects / immutable objects? switch? break? default? case continuation? continue? arrays? array indexing? default values of instance variables? length? ArrayIndexOutOfBoundsException? Array aliasing?CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland10Midterm ReviewWhat Is / Are …Array initializers? Array promotion during argument passing? Shallow / half-deep / deep copying of arrays? Privacy leaks? Model-View-Controller design pattern? Polymorphism? Interfaces? implements? Differences between interfaces, classes? Wrappers? <bexp> ? <exp1> : <exp2> ? algorithms? use case? precondition? postcondition?CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland11Midterm ReviewWhat Is / Are …API? Foo.java / Foo.class? javac? java? ClassPath? command-line arguments? Javadoc? Javadoc tags? two-dimensional arrays? ragged arrays? rectangular arrays? filenames and class names?CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland12Midterm ReviewWhat Is / Are …packages? subpackages? package hierarchy? fully qualified name? import? package? non-public classes? .jar files? exceptions? throwing / handling exceptions? try / catch / finally? stack trace? exception
View Full Document