Container Traversal Cmput 115 - Lecture 20 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.

Slides:



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

Containers CMPS Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary.
Lecture 5: Interfaces.
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.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 6 Object Oriented Programming in Java Language Basics Objects.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Cmput Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model.
Binary Search Trees Cmput Lecture 23 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Sorting - Selection Sort Cmput Lecture 10 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is.
Doubly-Linked Lists Cmput Lecture 16 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Ordered Containers Cmput Lecture 21 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Road Map Introduction to object oriented programming. Classes
Self-Reference - Induction Cmput Lecture 7 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
Data Abstraction - Interfaces and Implementations Cmput Lecture 1 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some.
Stacks Cmput Lecture 18 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.
Circularly-Linked Lists Cmput Lecture 17 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Sorting - Merge Sort Cmput Lecture 12 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Self-Reference - Recursion Cmput Lecture 6 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
Object (Data and Algorithm) Analysis Cmput Lecture 5 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Sorting - Insertion Sort Cmput Lecture 11 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is.
Quick Sort Cmput Lecture 13 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code.
Vectors Cmput Lecture 4 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is based on code from.
Code Clarity - Comments, Preconditions and Postconditions Cmput Lecture 2 Department of Computing Science University of Alberta ©Duane Szafron 1999.
Cmput Lecture 15 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book:
Queues Cmput Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.
The List Interface Cmput Lecture 14 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Dictionaries and Hash Tables Cmput Lecture 24 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture.
Abstract Data Types and Encapsulation Concepts
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
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.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Geoff Holmes Palindrome solutions Overview Arrays Collections Enumerators Vector BitSet Stack Dictionary Hashtable Collection Classes (Chapter 19) import.
Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction.
Ordered Containers CMPUT Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2003 Some code in this lecture is based.
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
What is Iterator Category: Behavioral Generic Way to Traverse Collection Not Related to Collection Implementation Details Not Related to the direction/fashion.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 13 Generics 1.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Collections Dwight Deugo Nesa Matic
Topic: Classes and Objects
Sixth Lecture ArrayList Abstract Class and Interface
EECE 310: Software Engineering
Lecture 12 Inheritance.
Review What is an object? What is a class?
Chapter 4: Writing Classes
Java Programming Language
slides created by Ethan Apter
Group Status Project Status.
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
slides created by Ethan Apter
Java Programming Language
Web Design & Development Lecture 6
slides created by Ethan Apter and Marty Stepp
Presentation transcript:

Container Traversal Cmput Lecture 20 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book: Java Structures by Duane A. Bailey or the companion structure package Revised 2/23/00

©Duane Szafron About This Lecture In this lecture we will learn about traversing containers. In Java, traversal is commonly done using the Enumeration and Iterator Interfaces.

©Duane Szafron Outline Traversals The Enumeration Interface The Iterator Interface Vector Iterators

©Duane Szafron Container Traversal traversal A traversal is an operation that accesses each element of a container once and performs some operation on the element as it is accessed. Common traversals: –Output all elements in a container. –Create a new container that contains all of the elements of the old one. –Compute the index of the first element of an indexed container that is equal to a key. –Add up the values of all Integer elements in a container.

©Duane Szafron Four Approaches to Traversal There are four common approaches to traversal: –Explicit traversal operations –Cursors –Method Parameters –Iterators

©Duane Szafron Explicit Operations The explicit traversal operations approach provide a different container operation for each traversal, like: outputElements(), findIndex (Object), sumOfElements(), capitalizeElements() One disadvantage of this approach is that similar traversal code is repeated in each traversal operation (e.g., we need code to output and to sum the elements in a container). A second disadvantage is that new traversals may be required after the container class has been finished.

©Duane Szafron Cursor Traversals cursor traversal The cursor traversal approach provide operations that give access to a current element (cursor) and operations that move the cursor, and let the user write the different traversals as needed. Example: cursor = this.head; // partial traversal while ((cursor != null) && (!cursor.value().equals(anObject)) cursor = cursor.next(); The disadvantage of this approach is that the user must write the traversals –The user may make mistakes. –This is duplication of effort for multiple users.

©Duane Szafron A Container with a Cursor aContainer firstcursor

©Duane Szafron A Container with a Cursor A cursor protocol: –resetCursor() –moveCursor() –cursorAtEnd() –cursorElement() aContainer cursorfirst

©Duane Szafron Method Parameters - apply method parameters Provide a few basic traversal operations that apply method parameters to the elements. We could add the method applyUsing(Object, Method) to a container class which takes an Object and a method as arguments and applies the method to the Object once for each element of the container with the element as an argument. Vector For example, if Vector provided this method we could print all of the elements in the Vector using: myVector.applyUsing(System.out, println(Object)) We could use the same method to copy the Vector: myVector.applyUsing(newVector, addElement(Object))

©Duane Szafron Method Parameters - find find(Method) We could add the method find(Method) to a container class which takes a method as an argument, applies the method to each element of the container and returns the index for the first application that returns true. For example, if Vector provided this method we could find the index of an Object using: myVector.find(equals(Object)) Unfortunately, Java does not support methods as parameters, but other languages (Pascal, C, C++ and Smalltalk) do.

©Duane Szafron Iterators and Enumerations The Iterator traversal approach is similar to the cursor approach, but a new class of object is provided so that the container class provider hides the cursor. Java introduces two interfaces to support iterator- style traversals: Enumeration and Iterator. The Enumeration Interface supports container traversal in which the order is not deterministic. The Iterator Interface supports ordered traversal of containers.

©Duane Szafron Enumeration Hierarchy In the java.* packages, Enumerator and Iterator are independent Interfaces. In the structure package, Iterator extends Enumerator. The Iterator Interface in the structure package defines different methods than the Iterator Interface in the java.* packages. Enumerator Iterator

©Duane Szafron Creating Enumerations & Iterators The user never creates an Enumeration or Iterator object explicitly. Instead, each container class provides a method that returns an object that implements the Enumeration or Iterator interface. For example, the Collection Interface defines: public Iterator elements(); // post: return an iterator for traversing the // collection

©Duane Szafron Enumeration & Iterator Classes The elements() method is implemented in each class that implements the Collection Interface. The class of the Object returned is hidden from the user of the collection class. The Enumeration or Iterator class is defined inside of the collection class, but is not made public. The Enumeration or Iterator object can only be used by sending it messages defined in the public interface.

©Duane Szafron Structure Interface - Enumerator public interface Enumerator { public boolean hasMoreElements(); // pre: the associated container has not been changed // since the last time this method was invoked // post: returns true iff at least one element of the // associated container has yet to be enumerated. public Object nextElement(); // pre: the associated container has more // unenumerated elements // post: returns an unenumerated element of the // associated container and marks it as enumerated } code based on Bailey pg. 155

©Duane Szafron Enumerator Example public static void main (String[ ] args) { Collection container; Enumeration enumerator; container = new SetList(); container.add(“Fred”); container.add(“Barney”); enumerator = container.elements(); while (enumerator.hasMoreElements()) System.out.println(enumerator.nextElement()); } code based on Bailey pg. 158 Barney Fred

©Duane Szafron Structure Interface - Iterator public interface Iterator extends Enumeration { public Object nextElement (); // pre: the associated container has more // uniterated elements // post: returns the next uniterated element of the // associated container and marks it as iterated public void reset (); // post: Makes the Iterator ready for a new traversal public Object value (); // pre: the associated container has more // uniterated elements // post: Returns the next element to be iterated } code based on Bailey pg. 157

©Duane Szafron Iterator Example public static void main (String[ ] args) { Vector container; Iterator iterator; container = new Vector(); container.addElement(“Fred”); container.addElement(“Barney”); iterator = container.elements(); while (iterator.hasMoreElements()) { System.out.println(iterator.value()); System.out.println(iterator.nextElement()); } code based on Bailey pg. 158 Fred Barney

©Duane Szafron VectorIterator - State and Constructor class VectorIterator implements Iterator { // This class is not a public class // It is implemented inside of the file: Vector.java that // declares the public class: Vector protected Vector vector; protected int current; public VectorIterator(Vector v) {// not really public // post: intitalizes Vector and resets the traversal this.vector = v; this.reset(); } code based on Bailey pg. 159

©Duane Szafron VectorIterator - Enumeration Interface /* Interface Enumeration Methods */ public boolean hasMoreElements () { // pre: the associated container has not been changed // since the last time this method was invoked // post: returns true iff at least one element of the // associated container has yet to be enumerated. return this.current < this.vector.size(); } public Object nextElement () { // pre: the associated container has more uniterated elements // post: returns the next uniterated element of the // associated container and marks it as iterated return this.vector.elementAt(this.current++); } code based on Bailey pg. 159

©Duane Szafron VectorIterator - Iterator Interface /* Interface Iterator Methods */ public void reset () { // post: Makes the Iterator ready for a new traversal this.current = 0; } public Object value (); // pre: the associated container has more // uniterated elements // post: Returns the next element to be iterated return this.vector.elementAt(this.current); } code based on Bailey pg. 160

©Duane Szafron Class Vector - elements() public Iterator elements () { // post: returns an Iterator for traversing the elements return new VectorIterator (this); } code based on Bailey pg. 158

©Duane Szafron Some Principles from the Textbook 14. Never modify a data structure while an associated Enumeration is live. 15. Assume that values returned by Iterators are read-only. principles from Bailey ch. 8