Iterators in Java. Lecture Objectives To understand the concepts of Java iterators To understand the differences between the Iterator and ListIterator.

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.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
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.
Java Threads Part II. Lecture Objectives To understand the concepts of multithreading in Java To be able to develop simple multithreaded applications.
Chapter 16 Collections, Maps and Iterators Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
© 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.
© 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.
Iterators Chapter 8 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
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.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
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.
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 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.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
CS2110 Recitation 07. Interfaces Iterator and Iterable. Nested, Inner, and static classes We work often with a class C (say) that implements a bag: unordered.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CS2110: SW Development Methods
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Linked Lists. RHS – SOC 2 Linked lists We can already store collec- tions of objects in arrays and array lists – why would we need other data structures…?
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.
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.
Computer Science 209 The Factory Pattern. Collections and Iterators List list1 = new ArrayList (); List list2 = new LinkedList (); Set set1 = new HashSet.
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.
Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
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.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
Computer Science 209 Software Development Inheritance and Composition.
CSE 1201 Object Oriented Programming ArrayList 1.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
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,
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.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
Lecture 8: Collections, Comparisons and Conversions. Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
CMSC 202 ArrayList Aug 9, 2007.
Java Generics.
University of Central Florida COP 3330 Object Oriented Programming
COMP 121 Week 9: ArrayList.
CMSC 202 ArrayList Aug 9, 2007.
Dynamic Data Structures and Generics
CMSC 202 ArrayList Aug 9, 2007.
Containers and Iterators
Comp 249 Programming Methodology
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Lecture Set 9 Arrays, Collections, and Repetition
Presentation transcript:

Iterators in Java

Lecture Objectives To understand the concepts of Java iterators To understand the differences between the Iterator and ListIterator interfaces To be able to implement Java programs based on Iterator and ListIterator interfaces

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

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();

Methods in the Iterator Interface

Methods in the Iterator Interface (Cont’d)

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 Methods in the Iterator Interface (Cont’d)

An Iterator

An Iterator (Cont’d)

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

For-Each Loops as Iterators (Cont’d)

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

Methods in the ListIterator Interface

Methods in the ListIterator Interface (Cont’d)

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

ListIterator Cursor Positions

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

An Iterator Returns a Reference

An Iterator Returns a Reference (Cont’d)

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