Iterators Dan Fleck.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

Computer Science 112 Fundamentals of Programming II List Iterators.
Double-Linked Lists and Circular Lists
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.
Lists and the Collection Interface Chapter 4 Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
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.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
CSE 143 Lecture 15 Sets and Maps; Iterators reading: ; 13.2; 15.3; 16.5 slides adapted from Marty Stepp
CS2110: SW Development Methods
1 Java's Collection Framework By Rick Mercer with help from The Java Tutorial, The Collections Trail, by Joshua BlockThe Collections Trail.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Collections –data structures and Algorithms L. Grewe.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Computer Science 209 The Factory Pattern. Collections and Iterators List list1 = new ArrayList (); List list2 = new LinkedList (); Set set1 = new HashSet.
COMP 121 Week 11: Linked Lists.
Lecture Objectives  Linked list data structures:  Singly-linked (cont.)  Doubly-linked  Circular  Implementing the List interface as a linked list.
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.
1 TCSS 342, Winter 2005 Lecture Notes Linked List Implementation Weiss Ch. 6, pp Weiss Ch. 17, pp
COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
Computer Science 209 Software Development Inheritance and Composition.
List Interface and Linked List Mrs. Furman March 25, 2010.
Iteration Abstraction SWE Software Construction Fall 2009.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
CSE 143 Lecture 12: Sets and Maps reading:
University of Limerick1 Collections The Collection Framework.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
CSE 143 Lecture 16 Iterators; Grammars reading: 11.1, 15.3, 16.5 slides created by Marty Stepp
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.
9/27/2016IT 1791 Abstraction A tool (concept) to manage complexity Hide irrelevant details; focus on the features needed Primitive date types are already.
Java Generics And collections
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs)
CSE 373 Implementing a Stack/Queue as a Linked List
The List ADT Reading: Textbook Sections 3.1 – 3.5
Chapter 19 Java Data Structures
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
ADT’s, Collections/Generics and Iterators
University of Central Florida COP 3330 Object Oriented Programming
Array List Pepper.
Iteration Abstraction
Linked Lists.
Back to Collections Lists: Sets Maps ArrayList LinkedList
"First things first, but not necessarily in that order " -Dr. Who
The List ADT Reading: Textbook Sections 3.1 – 3.5
(Java Collections Framework) AbstractSequentialList
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Arrays versus ArrayList
Copyright ©2012 by Pearson Education, Inc. All rights reserved
slides adapted from Marty Stepp
Java For-Each loop A delightful shortcut.
Iteration Abstraction
Lists and the Collection Interface
CSE 373 Java Collection Framework, Part 2: Priority Queue, Map
CSE 1020: The Collection Framework
JCF Collection classes and interfaces
Linked Lists Chapter 5 (continued)
TCSS 143, Autumn 2004 Lecture Notes
Part of the Collections Framework
CSC 205 – Java Programming II
Java Generics & Iterators
Introduction to Java Collection
Presentation transcript:

Iterators Dan Fleck

Iterators Iterator JCF defines generic interface java.util.Iterator Object that can access a collection of objects one object at a time Traverses the collection of objects JCF defines generic interface java.util.Iterator And a subinterface ListIterator

Iterator Example ArrayList aList = new ArrayList(); populateListWithStrings(aList); Iterator itr = aList.iterator(); while (itr.hasNext()) { String nextStr = (String)itr.next(); System.out.printf(“List item is: %s”, nextStr); }

Iterator Example with Generics ArrayList<String> aList = new ArrayList<String>(); populateListWithStrings(aList); Iterator<String> itr = aList.iterator(); while (itr.hasNext()) { String nextStr = itr.next(); System.out.printf(“List item is: %s”, nextStr); }

Iterator methods See Javadoc for Iterator Do all Iterators implement the remove operation? What happens if they don’t? What is another way to iterate through a Collection (ArrayList, Vector, Set, HashSet, LinkedList, etc…)? Answer: New for loop!

ListIterator Extends iterator to allow reverse iteration of a List “hasPrevious()”, “previous” methods Has an add() method Has a set() method to replace elements Lets you get the index of the element Int nextIndex(), int previousIndex()