Lecture 32 Iterators Last time 1 Collections 2 Stack T 3 ArrayList T Today 1 Midterm 11 15 2 Iterators 3 Review 11 13 2006 CMSC 131 Fall 2006 Rance Cleaveland 2006 Univeristy of Maryland Midterm 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 site CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 1 Iterators Special objects for iterating through elements in collection Created using iterator method of collection ArrayList String a Iterator String i a iterator Iterator objects implement Iterator T interface Methods of interest T next boolean hasNext void remove Return current element advance Is there a next element Remove current element Be sure to import java util Interface In array paradigm next equivalent to a i hasNext equivalent to i a length CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 2 Example public 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 2006 Rance Cleaveland 2006 University of Maryland 3 Iterator 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 java CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 4 Iterators and Mutability If you are iterating through a collection and someone adds or removes an element your iterator is 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 iterator CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 5 Mutable Strings Strings are immutable Sometime mutable strings would be handy Once a String object is created it cannot be altered For String objects shallow half deep deep copying why 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 Strings CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 6 StringBuffer Basics See documentation at http java sun com j2se 1 5 0 docs api java lang Stri ngBuffer 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 2006 Rance Cleaveland 2006 University of Maryland 7 Example What 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 2006 Rance Cleaveland 2006 University of Maryland 8 Midterm Review What 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 2006 Rance Cleaveland 2006 University of Maryland 9 Midterm Review What 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 2006 Rance Cleaveland 2006 University of Maryland 10 Midterm Review What 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 2006 Rance Cleaveland 2006 University of Maryland 11 Midterm Review What 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 propagation CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 12
View Full Document