Unformatted text preview:

Topic 14pIterators"First things first, but not necessarily itht d "in that order "-Dr Who-Dr. WhoCS 307 Fundamentals of Computer Science Iterators1A Questionbli l W dLi t {public class WordList {private ArrayList<String> myList; // pre: none// post: all words that are exactly len// characters long have been removed from// characters long have been removed from// this WordList with the order of the// remaining words unchangedpublic void removeWordsOfLength(int len){public void removeWordsOfLength(int len){for(int i = 0; i < myList.size(); i++){if( myList.get(i).length() == len )Li t (i)myList.remove(i);} CS 307 Fundamentals of Computer Science Iterators2Attendance Question 1When does method removeWordsOfLength work as intended?A. AlwaysBSometimesB.SometimesC. Never// original list = [“dog”, “cat”, “hat”, “sat”]// resulting list afterremoveWordsOfLength(3) ?// resulting list after removeWordsOfLength(3) ?CS 307 Fundamentals of Computer Science Iterators3The Remove QuestionA?Answer?public void removeWordsOfLength(int len) {Iterator<String> it = myList iterator();Iterator<String> it = myList.iterator();while( it.hasNext() )if( it.next().length() == len )it.remove();}}// original list = [“dog”, “cat”, “hat”, “sat”]// resulting list afterremoveWordsOfLength(3) ?CS 307 Fundamentals of Computer Science Iterators4// resulting list after removeWordsOfLength(3) ?Iterators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 orderSo eCo ec o sdo a eade eode– Sets, Maps, GraphsHow to access all the items in a CollectionHow to access all the items in a Collection with no specified order?CS 307 Fundamentals of Computer Science Iterators5Access All Elements - ArrayListiii iipublic 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 Iterators6Iterator InterfaceA it t bj t i “ h t” bj tAn iterator object is a “one shot” object– it is designed to go through all the elements of a Collection onceelements of a Collection once– if you want to go through the elements of a Collection again youelements of a Collection again you have to get another iterator objectIterators are obtained by calling yga method from the CollectionCS 307 Fundamentals of Computer Science Iterators7Iterator MethodsThe Iterator interface specifies 3 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//returns the next element in this iteration//pre: hastNext()id ()void remove()/*Removes from the underlying collection the last element returned by the iterator.Thi th d b ll d l ll t tpre: This method can be called only once per call to next. After calling, must call next again before calling remove again. */CS 307 Fundamentals of Computer Science Iterators8*/Attendance Question 2Which of the following produces a syntax error?ArrayList list = new ArrayList(); Iterator it1 = new Iterator(); // IIteratorit2 newIterator(list); // IIIteratorit2 = new Iterator(list); // II Iterator it3 = list.iterator(); // IIIAIA. IB. IICIIIC. IIID. I and IIE. II and IIICS 307 Fundamentals of Computer Science Iterators9Typical Iterator Patterniii iipublic void printAll(ArrayList list){Iterator it = list.iterator();Obj t tObject temp;while( it.hasNext() ) {itemp = it.next();System.out.println( temp );}}CS 307 Fundamentals of Computer Science Iterators10Typical Iterator Pattern 2public void printAll(ArrayList list){public void printAll(ArrayList list){Iterator it = list.iterator();while( it.hasNext() )S t t i tl ( it t() )System.out.println( it.next() );}// i// 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 Iterators11A Picture of an IteratorImagine a fence made up of fence posts and rail sectionsrailsrailsfencepostsCS 307 Fundamentals of Computer Science Iterators12Fence 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 theAs long as a rail exists to the right of the Iterator, hasNext() is trueiterator objectjCS 307 Fundamentals of Computer Science Iterators13Fence AnalogyALitStiArrayList<String> names = new ArrayList<String>();names.add(“Jan”);names.add(“Levi”);names.add(“Tom”);names add(“Jose”);names.add( Jose );Iterator<String> it = names.iterator();int i = 0;“Jan” “Levi” “Tom” “Jose”CS 307 Fundamentals of Computer Science Iterators14Fence Analogyiiwhile( it.hasNext() ) {i++;System.out.println( it.next() );}// when i == 1, prints out Janfirst call to next moves iterator tonext post and returns “Jan”“Jan” “Levi” “Tom” “Jose”next post and returns “Jan”CS 307 Fundamentals of Computer Science Iterators15Fence Analogyiiwhile( it.hasNext() ) {i++;System.out.println( it.next() );}// when i == 2, prints out Levi“Jan” “Levi” “Tom” “Jose”CS 307 Fundamentals of Computer Science Iterators16Fence Analogyiiwhile( it.hasNext() ) {i++;System.out.println( it.next() );}// when i == 3, prints out Tom“Jan” “Levi” “Tom” “Jose”CS 307 Fundamentals of Computer Science Iterators17Fence Analogyiiwhile( it.hasNext() ) {i++;System.out.println( it.next() );}// when i == 4, prints out Jose“Jan” “Levi” “Tom” “Jose”CS 307 Fundamentals of Computer Science Iterators18Fence Analogyiiwhile( it.hasNext() ) {i++;System.out.println( it.next() );}// call to hasNext returns false// while loop stops“Jan” “Levi” “Tom” “Jose”CS 307 Fundamentals of Computer Science Iterators19Attendance Question 3What is output by the following code?ArrayList<Integer> list;List = new ArrayList<Integer>();list.add(3);list


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?