© 2006 Pearson Addison-Wesley. All rights reserved16-1 Iterators An iterator is an object that is used with a collection to provide sequential access to.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Generics and the ArrayList Class
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
CS102--Object Oriented Programming Lecture 17: – Linked Lists Copyright © 2008 Xiaoyan Li.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Chapter 16 Collections, Maps and Iterators Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Threads Part I.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
© 2006 Pearson Addison-Wesley. All rights reserved16-1 Methods in the List Interface (Part 1 of 16)
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 The final Modifier A method marked final indicates that it cannot be overridden with a new definition.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Iterators Chapter 8 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Java Collections. Lecture Objectives To understand the concepts of Java collections To be able to implement Java programs based on Collections.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Iterators in Java. Lecture Objectives To understand the concepts of Java iterators To understand the differences between the Iterator and ListIterator.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 8 Polymorphism Part I.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
1 Introduction to Searching and Sorting Comparable Interface -Reading p Comparator Interface.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Lists and the Collection Interface Review inheritance & collections.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Slides prepared by Rose Williams, Binghamton University Chapter 15 Linked Data Structures.
Holding Your Objects Generics and type-safe containers One of the problems of using pre-Java SE5 containers was that the compiler allowed.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
CSE 1201 Object Oriented Programming ArrayList 1.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
Interfaces. In order to work with a class, you need to understand the public methods  methods, return types,…  after you instantiate, what can you do.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 21 Sets and Maps.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Collections, Maps and Iterators
CMSC 202 ArrayList Aug 9, 2007.
University of Central Florida COP 3330 Object Oriented Programming
CS240: Advanced Programming Concepts
Comp 249 Programming Methodology
Copyright ©2012 by Pearson Education, Inc. All rights reserved
CMSC 202 ArrayList Aug 9, 2007.
CMSC 202 ArrayList Aug 9, 2007.
Josephus.
Containers and Iterators
Comp 249 Programming Methodology
Presentation transcript:

© 2006 Pearson Addison-Wesley. All rights reserved16-1 Iterators An iterator is an object that is used with a collection to provide sequential access to the collection elements –This access allows examination and possible modification of the elements An iterator imposes an ordering on the elements of a collection even if the collection itself does not impose any order on the elements it contains –If the collection does impose an ordering on its elements, then the iterator will use the same ordering

© 2006 Pearson Addison-Wesley. All rights reserved16-2 The Iterator Interface Java provides an Iterator interface –Any object of any class that satisfies the Iterator interface is an Iterator An Iterator does not stand on its own –It must be associated with some collection object using the method iterator –If c is an instance of a collection class (e.g., HashSet ), the following obtains an iterator for c : Iterator iteratorForC = c.iterator();

© 2006 Pearson Addison-Wesley. All rights reserved16-3 Methods in the Iterator Interface (Part 1 of 2)

© 2006 Pearson Addison-Wesley. All rights reserved16-4 Methods in the Iterator Interface (Part 2 of 2)

© 2006 Pearson Addison-Wesley. All rights reserved16-5 Using an Iterator with a HashSet Object A HashSet object imposes no order on the elements it contains However, an iterator will impose an order on the elements in the hash set –That is, the order in which they are produced by next() –Although the order of the elements so produced may be duplicated for each program run, there is no requirement that this must be the case

© 2006 Pearson Addison-Wesley. All rights reserved16-6 An Iterator (Part 1 of 3)

© 2006 Pearson Addison-Wesley. All rights reserved16-7 An Iterator (Part 2 of 3)

© 2006 Pearson Addison-Wesley. All rights reserved16-8 An Iterator (Part 3 of 3)

© 2006 Pearson Addison-Wesley. All rights reserved16-9 Tip: For-Each Loops as Iterators Although it is not an iterator, a for-each loop can serve the same purpose as an iterator –A for-each loop can be used to cycle through each element in a collection For-each loops can be used with any of the collections discussed here

© 2006 Pearson Addison-Wesley. All rights reserved16-10 For-Each Loops as Iterators (Part 1 of 2)

© 2006 Pearson Addison-Wesley. All rights reserved16-11 For-Each Loops as Iterators (Part 2 of 2)

© 2006 Pearson Addison-Wesley. All rights reserved16-12 The ListIterator Interface The ListIterator interface extends the Iterator interface, and is designed to work with collections that satisfy the List interface –A ListIterator has all the methods that an Iterator has, plus additional methods –A ListIterator can move in either direction along a list of elements –A ListIterator has methods, such as set and add, that can be used to modify elements

© 2006 Pearson Addison-Wesley. All rights reserved16-13 Methods in the ListIterator Interface (Part 1 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved16-14 Methods in the ListIterator Interface (Part 2 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved16-15 Methods in the ListIterator Interface (Part 3 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved16-16 Methods in the ListIterator Interface (Part 4 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved16-17 The ListIterator Cursor Every ListIterator has a position marker known as the cursor –If the list has n elements, they are numbered by indices 0 through n-1, but there are n+1 cursor positions –When next() is invoked, the element immediately following the cursor position is returned and the cursor is moved forward one cursor position –When previous() is invoked, the element immediately before the cursor position is returned and the cursor is moved back one cursor position

© 2006 Pearson Addison-Wesley. All rights reserved16-18 ListIterator Cursor Positions

© 2006 Pearson Addison-Wesley. All rights reserved16-19 Pitfall: next and previous Can Return a Reference Theoretically, when an iterator operation returns an element of the collection, it might return a copy or clone of the element, or it might return a reference to the element Iterators for the standard predefined collection classes, such as ArrayList and HashSet, actually return references –Therefore, modifying the returned value will modify the element in the collection

© 2006 Pearson Addison-Wesley. All rights reserved16-20 An Iterator Returns a Reference (Part 1 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved16-21 An Iterator Returns a Reference (Part 2 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved16-22 An Iterator Returns a Reference (Part 3 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved16-23 An Iterator Returns a Reference (Part 4 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved16-24 Tip: Defining Your Own Iterator Classes There is usually little need for a programmer defined Iterator or ListIterator class The easiest and most common way to define a collection class is to make it a derived class of one of the library collection classes –By doing this, the iterator() and listIterator() methods automatically become available to the program If a collection class must be defined in some other way, then an iterator class should be defined as an inner class of the collection class