Unformatted text preview:

Topic 14 IteratorsA QuestionAttendance Question 1The Remove QuestionIteratorsAccess All Elements - ArrayListIterator InterfaceIterator MethodsAttendance Question 2Typical Iterator PatternTypical Iterator Pattern 2A Picture of an IteratorFence AnalogySlide 14Slide 15Slide 16Slide 17Slide 18Slide 19Attendance Question 3Comodificationremove methodCommon Iterator ErrorThe Iterable InterfaceIterableImplementing an IteratorCS 307 Fundamentals of Computer Science Iterators1Topic 14Iterators"First things first, but not necessarily in that order "-Dr. WhoCS 307 Fundamentals of Computer Science Iterators2A Questionpublic class WordList {private ArrayList<String> myList; // pre: none// post: all words that are exactly len// characters long have been removed from// this WordList with the order of the// remaining words unchangedpublic void removeWordsOfLength(int len){for(int i = 0; i < myList.size(); i++){if( myList.get(i).length() == len )myList.remove(i);}Attendance Question 1When does method removeWordsOfLength work as intended?A.AlwaysB.SometimesC.Never// original list = [“dog”, “cat”, “hat”, “sat”]// resulting list after removeWordsOfLength(3) ?CS 307 Fundamentals of Computer Science Iterators3CS 307 Fundamentals of Computer Science Iterators4The Remove QuestionAnswer?public void removeWordsOfLength(int len) {Iterator<String> it = myList.iterator();while( it.hasNext() )if( it.next().length() == len )it.remove();}}// original list = [“dog”, “cat”, “hat”, “sat”]// resulting list after removeWordsOfLength(3) ?CS 307 Fundamentals of Computer Science Iterators5IteratorsArrayList is part of the Java Collections frameworkCollection is an interface that specifies the basic operations every collection (data structure) should haveSome Collections don’t have a definite order–Sets, Maps, GraphsHow to access all the items in a Collection with no specified order?CS 307 Fundamentals of Computer Science Iterators6Access All Elements - ArrayListpublic void printAll(ArrayList list){ for(int i = 0; i < list.size(); i++) System.out.println(list.get(i));}How do I access all the elements of a Set? The elements don’t have an index.Iterator objects provide a way to go through all the elements of a Collection, one at a timeCS 307 Fundamentals of Computer Science Iterators7Iterator InterfaceAn iterator object is a “one shot” object–it is designed to go through all the elements of a Collection once–if you want to go through the elements of a Collection again you have to get another iterator objectIterators are obtained by calling a method from the CollectionCS 307 Fundamentals of Computer Science Iterators8Iterator MethodsThe Iterator interface specifies 3 methods:boolean hasNext()//returns true if this iteration has more elementsObject next()//returns the next element in this iteration//pre: hastNext()void remove()/*Removes from the underlying collection the last element returned by the iterator. pre: This method can be called only once per call to next. After calling, must call next again before calling remove again. */Attendance Question 2Which of the following produces a syntax error?ArrayList list = new ArrayList(); Iterator it1 = new Iterator(); // IIterator it2 = new Iterator(list); // II Iterator it3 = list.iterator(); // IIIA. IB. IIC. IIID. I and IIE. II and IIICS 307 Fundamentals of Computer Science Iterators9CS 307 Fundamentals of Computer Science Iterators10Typical Iterator Patternpublic void printAll(ArrayList list){ Iterator it = list.iterator();Object temp;while( it.hasNext() ) {temp = it.next();System.out.println( temp );}}CS 307 Fundamentals of Computer Science Iterators11Typical Iterator Pattern 2public void printAll(ArrayList list){ Iterator it = list.iterator(); while( it.hasNext() ) System.out.println( it.next() );}// go through twice?public void printAllTwice(ArrayList list){ Iterator it = list.iterator(); while( it.hasNext() ) System.out.println( it.next() ); it = list.iterator(); while( it.hasNext() ) System.out.println( it.next() );}CS 307 Fundamentals of Computer Science Iterators12A Picture of an IteratorImagine a fence made up of fence posts and rail sectionsfencepostsrailsCS 307 Fundamentals of Computer Science Iterators13Fence AnalogyThe iterator lives on the fence postsThe data in the collection are the railsIterator created at the far left postAs long as a rail exists to the right of the Iterator, hasNext() is trueiterator objectCS 307 Fundamentals of Computer Science Iterators14Fence AnalogyArrayList<String> names = new ArrayList<String>();names.add(“Jan”);names.add(“Levi”);names.add(“Tom”);names.add(“Jose”);Iterator<String> it = names.iterator();int i = 0;“Jan” “Levi” “Tom” “Jose”CS 307 Fundamentals of Computer Science Iterators15Fence Analogywhile( it.hasNext() ) { i++; System.out.println( it.next() );}// when i == 1, prints out Jan“Jan” “Levi” “Tom” “Jose”first call to next moves iterator tonext post and returns “Jan”CS 307 Fundamentals of Computer Science Iterators16Fence Analogywhile( it.hasNext() ) { i++; System.out.println( it.next() );}// when i == 2, prints out Levi“Jan” “Levi” “Tom” “Jose”CS 307 Fundamentals of Computer Science Iterators17Fence Analogywhile( it.hasNext() ) { i++; System.out.println( it.next() );}// when i == 3, prints out Tom“Jan” “Levi” “Tom” “Jose”CS 307 Fundamentals of Computer Science Iterators18Fence Analogywhile( it.hasNext() ) { i++; System.out.println( it.next() );}// when i == 4, prints out Jose“Jan” “Levi” “Tom” “Jose”CS 307 Fundamentals of Computer Science Iterators19Fence Analogywhile( it.hasNext() ) { i++; System.out.println( it.next() );}// call to hasNext returns false// while loop stops“Jan” “Levi” “Tom” “Jose”Attendance Question 3What is output by the following code?ArrayList<Integer> list;List = new ArrayList<Integer>();list.add(3);list.add(3);list.add(5);Iterator<Integer> it = list.iterator();System.out.println(it.next());System.out.println(it.next());A.3 B. 5 C. 3 3 5D. 3 3 E. 3 5CS 307 Fundamentals of Computer Science Iterators20CS 307 Fundamentals of Computer Science Iterators21ComodificationIf a


View Full Document

UT CS 307 - Topic 14 Iterators

Documents in this Course
Midterm 2

Midterm 2

15 pages

Midterm 1

Midterm 1

15 pages

Syllabus

Syllabus

24 pages

s

s

8 pages

Midterm 1

Midterm 1

14 pages

Midterm 2

Midterm 2

14 pages

Recursion

Recursion

14 pages

Midterm 1

Midterm 1

16 pages

Load more
Download Topic 14 Iterators
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Topic 14 Iterators and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Topic 14 Iterators 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?