Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006.

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

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
The List ADT Textbook Sections
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.
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++)
Working With Collections in the AP ™ Java Subset 2003 ACTE Convention © 2003 by Kenneth A. Lambert.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
COMP 121 Week 11: Linked Lists. Objectives Understand how single-, double-, and circular-linked list data structures are implemented Understand the LinkedList.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
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.
CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
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.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Data Structures & Java Collections Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
CS 280 Data Structures Professor John Peterson. Project 9 Questions? IS280.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
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.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Iterators CS 367 – Introduction to Data Structures.
Iterator COMP 401, Spring 2013 Lecture 07 1/31/2013.
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…?
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
1 Java: AP Curriculum Focus and Java Subset Alyce Brady.
Ordered Containers CMPUT Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2003 Some code in this lecture is based.
CSC 205 Programming II Lecture 18 The Eight Queens Problem.
LinkedList Many slides from Horstmann modified by Dr V.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
1 Iterators "First things first, but not necessarily in that order " -Dr. Who CS Computer Science II.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. An Introduction to Data Structures.
Information and Computer Sciences University of Hawaii, Manoa
(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.
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)
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
COMP 121 Week 11: Linked Lists.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
Lists and Iterators Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming.
Computer Science 209 Software Development Inheritance and Composition.
List Interface and Linked List Mrs. Furman March 25, 2010.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
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.
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
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.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
Recursion CS 180 Department of Computer Science Purdue University.
Iterators.
Java Methods Lists and Iterators Object-Oriented Programming
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Linked Lists.
"First things first, but not necessarily in that order " -Dr. Who
CSE 143 Lecture 27: Advanced List Implementation
Iterator.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Interator and Iterable
CSC 205 – Java Programming II
Presentation transcript:

Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class,

List Traversal What is it? –A way of moving through linked data structures node by node –It can be used with plenty of classes, such as LinkedList, Set, etc.

Methods of Traversal Using the getNext() method of the ListNode class creating a current instance variable that refers to a particular node in a linked structure

But, There are some problems!!!!!!!!! For Example, what happens if you want to access the list in two places simultaneously?

The solution is... The Iterator!

So, What is an iterator? Iterator is an iterface in the java.util package. Iterators advance through the nodes of a linked data structure one by one

Methods of Iterator boolean hasNext() –returns true if there are more elements to be examined, false otherwise Object next() –returns the next element void remove() –removes the last element returned by next

hasNext() Current In this case hasNext() would return true

hasNext() Current In this case hasNext() would return false

next() The first call to next() results in the first element of the the data structure. To begin with, the list looks like this Current

next() When next is called, it looks like this Current

next() When next is called again, it looks like this Current

next() When next is called again, it looks like this Current If next is called one more time, an exception is thrown

remove() remove() can only be used once per call to next() next() must be called at least once before remove() is called If next() is not called before, an exception is thrown.

remove() If a the current node is position one (meaning next() would result in pos. 2) and remove() is called... Current 12 3

remove() The linked list would look like this Current 23

ListIterator ListIterator is an extension of the Iterator interface It has two additional methods: void set(Object o) and add(Object o)

set(Object o) Sets the current node to Object o. For example: Current xyz With this LinkedList, a call to set(w) would produce...

This! Current x w z

add(Object o) Adds a node immediately before what would be the result of next() Current x w z For example, a call of add(v) would produce...

This! Current x wzv

Creating an instance interator Oftentimes, an instance of Iterator of ListIterator can be created by using the iterator() or listIterator() method of a class. For LinkedList g, Iterator x = g.iterator() would produce an instance of Iterator called x that begins at g’s first node. On your AP Test, refer to the java subset to see which classes have these methods.

Bibliography Barron’s How to Prepare for the AP Computer Science Advanced Placement Examination Java Version util/Iterator.html