What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

Similar documents
CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

COMP 250. Lecture 29. interfaces. Nov. 18, 2016

Java Review: Objects

Basic Data Structures

Basic Data Structures 1 / 24

Outline. iterator review iterator implementation the Java foreach statement testing

Programming II (CS300)

Introduction to Computer Science I

Algorithms. Produced by. Eamonn de Leastar

Dynamic Data Structures and Generics

2. The actual object type stored in an object of type CollectionClassName<E> is specified when the object is created.

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

Computational Expression

Abstract Data Types (ADTs) Example ADTs. Using an Abstract Data Type. Class #08: Linear Data Structures

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

Name Section Number. CS210 Exam #4 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

ITI Introduction to Computing II

Lists. ordered lists unordered lists indexed lists 9-3

ITI Introduction to Computing II

ITI Introduction to Computing II

Polymorphism: Interfaces and Iteration. Fundamentals of Computer Science

CMSC 132: Object-Oriented Programming II

11/2/ Dynamic Data Structures & Generics. Objectives. Array-Based Data Structures: Outline. Harald Gall, Prof. Dr.

CSE 143 Lecture 14. Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4

Programming II (CS300)

Class 26: Linked Lists

Lecture 9: Lists. MIT-AITI Kenya 2005

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes.

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1

ITI Introduction to Computing II

Java Collections. Readings and References. Collections Framework. Java 2 Collections. References. CSE 403, Winter 2003 Software Engineering

What is the Java Collections Framework?

//instance variables //methods. Foo x = new Foo(); Interface: also a type of objects public interface Bar {

Title Description Participants Textbook

Iterators. Chapter 15. Copyright 2012 by Pearson Education, Inc. All rights reserved

Today: Java Library Classes for lists. Iterators, ListIterators. CS61B Lecture #7. Last modified: Fri Sep 12 14:41: CS61B: Lecture #7 1

Collections and Iterators. Collections

Java Collections Framework: Interfaces

Lab 5 Random numbers!

CS 3 Introduction to Software Engineering. 5: Iterators

The Java Collections Framework and Lists in Java Parts 1 & 2

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables

Software Construction

Data Structures Brett Bernstein

EECS 2011 M: Fundamentals of Data Structures

Inner Classes. CMSC 433 Programming Language Technologies and Paradigms Spring Example: The Queue Class. Example: The Queue Class (cont d)

Announcements/Follow-ups

Interfaces and itera-on. CSCI 136: Fundamentals of Computer Science II Keith Vertanen

CSC 321: Data Structures. Fall 2017

CS11 Java. Winter Lecture 8

CS : Data Structures Michael Schatz. Sept Lecture 5: Iterators

Arrays. Chapter Arrays What is an Array?

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

CS 2230 CS II: Data structures. Meeting 26: the Set ADT Brandon Myers University of Iowa

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

Java Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering

CS 340 Homework 4. Due Wednesday November 1

An Introduction to Data Structures

Lists and Iterators. CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology

JAVA COLLECTION FRAMEWORK & SETS

Interruptible Iterators

Implementing a List in Java. CSE 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List.

Lists. The List ADT. Reading: Textbook Sections

List ADT. B/W Confirming Pages

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Java Collection Framework

CS Introduction to Data Structures Week 1 Thursday

Recursive Objects. Singly Linked List (Part 2)

CS 151. Exceptions & Javadoc. slides available on course website. Sunday, September 9, 12

Some examples and/or figures were borrowed (with permission) from slides prepared by Prof. H. Roumani. The Collection Framework

CSC 1052 Algorithms & Data Structures II: Lists

Introduction to Computer Science II (ITI 1121) Final Examination

Higher-Order Sequential Operations

Collections and Maps

Active Learning: Streams

Queues. Data Structures and Design with Java and JUnit Rick Mercer 18-1

Data Structures and Algorithms

Overview of Java ArrayList, HashTable, HashMap, Hashet,LinkedList

Objects and Iterators

Week 16: More on Collections and Immutability

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

Implementing a List in Java. CSC 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List CSC

COSC 123 Computer Creativity. Java Lists and Arrays. Dr. Ramon Lawrence University of British Columbia Okanagan

CS S-05 Abstract Data Types and Lists 1

Lecture 8: Iterators and More Mutation

CIS 120 Final Exam May 3, Name (printed): Pennkey (login id):

Abstract Data Types. Data Str. Client Prog. Add. Rem. Find. Show. 01/22/04 Lecture 4 1

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018

Abstract Data Types. Data Str. Client Prog. Add. Rem. Find. Show. 01/30/03 Lecture 7 1

Grouping objects Lecture 7

COMP-202. Generics. COMP Generics, 2013 Jörg Kienzle and others

CS1083 Week 2: Arrays, ArrayList

Topic 10: The Java Collections Framework (and Iterators)

» Access elements of a container sequentially without exposing the underlying representation

Computational Expression

Linked Lists. Linked List Nodes. Walls and Mirrors Chapter 5 10/25/12. A linked list is a collection of Nodes: item next -3.

Java Language Features

CITS1001 week 4 Grouping objects lecture 2

Transcription:

Iterators

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one 9-2 2-2

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one Operations next: next element of the collection; ERROR if the element does not exist hasnext: true if there are more elements in the collection; false otherwise remove: removes the last element returned by the iterator 9-3 2-3

Consider an iterator for a collection storing the following elements: 5 9 23 34 9-4 2-4

Consider an iterator for a collection storing the following elements: 5 9 23 34 next: 9-5 2-5

Consider an iterator for a collection storing the following elements: 5 9 23 34 next: 5 9-6 2-6

Consider an iterator for a collection storing the following elements: 5 9 23 34 next: 9-7 2-7

Consider an iterator for a collection storing the following elements: 5 9 23 34 next: 9 9-8 2-8

Consider an iterator for a collection storing the following elements: 5 9 23 34 hasnext: 9-9 2-9

Consider an iterator for a collection storing the following elements: 5 9 23 34 hasnext: true 9-10 2-10

Consider an iterator for a collection storing the following elements: 5 9 23 34 remove 9-11 2-11

Consider an iterator for a collection storing the following elements: 5 23 34 remove 9-12 2-12

Consider an iterator for a collection storing the following elements: 5 23 34 next: 23 9-13 2-13

Consider an iterator for a collection storing the following elements: 5 23 34 next: 34 9-14 2-14

Iterator Interface public interface Iterator<T> { public boolean hasnext( ); public T next( ); public void remove( ); // (optional operation) } It is in the java.util package of the Java API 9-15 2-15

Array Iterator Consider a collection of data items stored in an array 0 1 2 3 4 5 null null null items 4 count 9-16

Array Iterator Consider a collection of data items stored in an array 0 1 2 3 4 5 null null null items 4 count 0 current 9-17

Array Iterator Consider a collection of data items stored in an array 0 1 2 3 4 5 null null null items 4 count 1 current next 9-18

// Represents an iterator over the elements of an array import java.util.*; public class ArrayIterator<T> implements Iterator<T> { // Attributes private int count; // number of elements in collection private int current; // current position in the iteration private T[ ] items; // items in the collection // Constructor: sets up this iterator using the // specified items public ArrayIterator (T[ ] collection, int size) { items = collection; count = size; current = 0; } 9-19 2-19

// Returns true if this iterator has at least one // more element to deliver in the iteration public boolean hasnext( ) { return (current < count); } } // Returns the next element in the iteration. // If there are no more elements in this iteration, // throws an exception. public T next( ) { if (! hasnext( )) throw new NoSuchElementException( ); current++; return items[current - 1]; } 9-20 2-20

Linked Iterator Consider a collection of data items stored in a linked list. head 9-21

Linked Iterator Consider a collection of data items stored in a linked list. current head next 9-22

Linked Iterator Consider a collection of data items stored in a linked list. current head next 9-23

import java.util.*; public class LinkedIterator<T> implements Iterator<T> { // Attributes private LinearNode<T> current; // current position } // Constructor: Sets up this iterator public LinkedIterator (LinearNode<T> collection){ current = collection; 9-24

} // Returns true if this iterator has at least one more element // to deliver in the iteration. public boolean hasnext( ) { return (current!= null); } // Returns the next element in the iteration. If there are no // more elements in this iteration, throws an exception. public T next( ) { if (! hasnext( )) throw new NoSuchElementException( ); T result = current.getelement( ); current = current.getnext( ); return result; } 9-25

Iterators for a Collection A List ADT can be implemented using, for example, an array or a linked list. For each implementation we can add an iterator operation that returns an iterator for the corresponding list. 9-26

/** iterator method for ArrayList * Returns an iterator for the elements currently in this list. * * @return an iterator for the elements in this list */ public Iterator<T> iterator() { return new ArrayIterator<T> (list, size); } 9-27 2-27

/** iterator method for ArrayList * Returns an iterator for the elements currently in this list. * * @return an iterator for the elements in this list */ public Iterator<T> iterator() { return new ArrayIterator<T> (list, size); } An application can then declare an iterator as ArrayList<String> a = new ArrayList<String>();... Iterator<String> iter = a.iterator(); 9-28 2-28

iterator method for LinkedList /** * Returns an iterator for the elements currently in this list. * @return an iterator for the elements in this list */ public Iterator<T> iterator( ) { return new LinkedIterator<T> (list); } An application can declare an iterator as LinkedList<String> list = new LinkedList<String>();... Iterator<String> iter = list.iterator(); 9-29

Using an Iterator in an Application If we want to print the elements in the iterator we can use this code: while(iter.hasnext()) { System.out.println(iter.next()); } This will work regardless of whether iter was obtained from thearraylist or from the LinkedList! 9-30

Why use Iterators? Traversing through the elements of a collection is very common in programming, and iterators provide a uniform way of doing so. Advantage? Using an iterator, we don t need to know how the collection is implemented! 9-31 2-31