Tirgul OOP No.3 Iterators. What is an Iterator? An object that provides a way to access elements of an aggregate object sequentially, without exposing.

Slides:



Advertisements
Similar presentations
Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Advertisements

Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of.
Double-Linked Lists and Circular Lists
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
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.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
COMP 121 Week 11: Linked Lists. Objectives Understand how single-, double-, and circular-linked list data structures are implemented Understand the LinkedList.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
1 Lecture 24 ADT Part V (Linked List Using Iterator) Overview  Utility Classes.  List Iterator.  View of the List Iterator.  Adding to the Head of.
Algorithm Programming Containers in Java Bar-Ilan University תשס " ו by Moshe Fresko.
Iterators Chapter 7. Chapter Contents What is an Iterator? A Basic Iterator Visits every item in a collection Knows if it has visited all items Doesn’t.
Data Structures & Java Collections Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 280 Data Structures Professor John Peterson. Project 9 Questions? IS280.
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
15-Jul-15 Generics. ArrayList s and arrays A ArrayList is like an array of Object s, but... Arrays use [ ] syntax; ArrayList s use object syntax An ArrayList.
Iterators CS 367 – Introduction to Data Structures.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
EECE 310: Software Engineering Iteration Abstraction.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Fifteen: An Introduction to Data Structures.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. An Introduction to Data Structures.
Topic 19 Binary Search Trees "Yes. Shrubberies are my trade. I am a shrubber. My name is 'Roger the Shrubber'. I arrange, design, and sell shrubberies."
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Today’s Agenda  Generic  Iterators CS2336: Computer Science II.
Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class,
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 16 – Basic Data Structures.
Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures.
CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
M180: Data Structures & Algorithms in Java Linked Lists – Part 2 Arab Open University 1.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 16 – Basic Data Structures.
Building Java Programs Binary Trees reading: 17.1 – 17.3.
Chapter 15 An Introduction to Data Structures. Chapter Goals To learn how to use the linked lists provided in the standard library To be able to use iterators.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 15 – An Introduction to Data Structures.
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
Recursive Objects Singly Linked List (Part 2) 1. Operations at the head of the list  operations at the head of the list require special handling because.
1 Iterator Pattern (A Behavioral Pattern) Prepared by: Neha Tomar.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
Iterators.
Lecture 19: Binary Trees reading: 17.1 – 17.3
Recursive Objects (Part 4)
Building Java Programs
Data Structures Lakshmish Ramaswamy.
Announcements & Review
Algorithm for deleting a node from a singly linked list
Chapter 16 – Basic Data Structures
Introduction to Data Structures
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Chapter 15 – An Introduction to Data Structures
Behavioral Patterns Part-I introduction UNIT-VI
Linked Lists [AJ 15].
Building Java Programs
Recursive Objects Singly Linked Lists.
ITI Introduction to Computing II Lab-12
Building Java Programs
Software Design Lecture : 39.
Java Generics & Iterators
Presentation transcript:

Tirgul OOP No.3 Iterators

What is an Iterator? An object that provides a way to access elements of an aggregate object sequentially, without exposing it ’ s internal structure

Example Suppose we have a myList object, which implements the List interface : public void remove(Object item); public void add(Object item); public Object elementAt(int index); We would like to print all of the elements in the Cartesian product (myList X mylist)

Naive Solution - 1 Naive Solution - 1 Add a function to the list interface which performs the task. Why is that bad ? – –What about myList X myList X myList ? – –What if we change the list implementation ?

Naive Solution - 2 Naive Solution - 2 void printAllPairs(IntList list) { for(int i=0; i<list.size(); i++) { int e1 = list.elementAt(i); for( int j=0; j<list.size(); j++) { int e2 = list.elementAt(j); System.out.println (“ ”); } Pros and cons?

Outline Definition Motivation Basic Implementation and usage Implementation issues

Declaring an Iterator The Java iterator interface public class Iterator { public Iterator(); public boolean hasNext(); public Object next(); } Create a ListIterator implementation for List Add the following to the List interface: public Iterator iterator() …

Implementation Assuming Linked List public class LinkedList... {... private class LinkedListIterator... { Node _position;... LinkedListIterator(Node head) { _position = head; }...

Implementation Assuming Linked List public Object next() {... // Check validity Object ret = _position._data; _position = _position._next; return ret; } public boolean hasNext() { return _position != null; } public Iterator iterator() { return new LinkedListIterator(_head); } }

Using an Iterator void printAllPairs(List list) { Iterator i1; for (i1 = list.iterator(); i1.hasNext(); ){ Iterator i2; Object e1 = i1.next(); for (i2 = list.iterator(); i2.hasNext();){ Object e2 = i2.next(); System.out.println(“ ”); }

Outline Definition Motivation Basic Implementation and usage Implementation issues: – –Who defines the traversal algorithm? – –Using auxiliary data structures – –Access rights of an iterator – –Iterators for composites

Who defines the traversal algorithm? Options : The aggregate object –Next operation belongs to the aggregate object –The iterator merely points to the current position The iterator –Better separation from the point of view of the client –Might require privileged access rights

Using an Auxiliary Data Structure public class List... {... public Iterator filtIterator(Object fltr){ List auxList = new List; Iterator i; for (i = iterator(); i.hasNext() ; ) { Object curItem = i.next(); if (!curItem.equals(fltr)) auxList.add(curItem) ; } return new FiltIterator(auxList); }... }

Iterator Defined Traversal - 1 public class List {... private class FilteredIterator... { Node _current; Object _filter; public FilteredIterator (Node head, Object filter) { _current = head; _filter = filter; advanceToNext(); } public boolean hasNext() { return (_current != null); } Note the access rights of the iterator

Iterator Defined Traversal - 2 Iterator Defined Traversal - 2 public Object next() { advanceToNext();... // Check validity return _current; } void advanceToNext() { boolean flag = true; while(flag && _current != null) if( _current.data.equals(_filter) ) _current = _current.next; else flag = false; }

Designing Iterators for Trees Options: Store a path to keep track of the current positions Use an auxiliary list Enhance the composite data structure – –Add access to children, parents and siblings Use an internal iterator