"First things first, but not necessarily in that order " -Dr. Who

Slides:



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

ArrayLists The lazy mans array. Whats the matter here? int[] list = new int[10]; list[0] = 5; list[2] = hey; list[3] = 15; list[4] = 23;
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.
Grouping objects Iterators. Iterator and iterator() Collections have an iterator() method. This returns an Iterator object. Iterator has three methods:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Bag implementation Add(T item) – Enlarge bag if necessary; allocate larger array Remove(T item) – Reduce bag if necessary; allocate smaller array Iterator.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Grouping Objects 3 Iterators, collections and the while loop.
Generic Classes and 'for each' Loops List zoo = new ArrayList (); // … add several Animals to the zoo for (Animal animal : zoo) // do something with animal.
CS 280 Data Structures Professor John Peterson. Project 9 Questions? IS280.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
1 Topic 8 Iterators "First things first, but not necessarily in that order " -Dr. Who.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
CS 307 Fundamentals of Computer ScienceIterators 1 Topic 14 Iterators "First things first, but not necessarily in that order " -Dr. Who.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
CS2110 Recitation 07. Interfaces Iterator and Iterable. Nested, Inner, and static classes We work often with a class C (say) that implements a bag: unordered.
Arrays And ArrayLists - S. Kelly-Bootle
Programming With Java ICS201 University Of Ha’il1 Chapter 14 Generics and The ArrayList Class.
Data structures Abstract data types Java classes for Data structures and ADTs.
The Java Collections Framework By the end of this lecture you should be able to: use the ArrayList class to store a list of objects; use the HashSet class.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
1 Iterators "First things first, but not necessarily in that order " -Dr. Who CS Computer Science II.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
JAVA COLLECTIONS M. TAIMOOR KHAN (ADAPTED FROM SWINBURNE NOTES)
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class,
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
List Interface and Linked List Mrs. Furman March 25, 2010.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
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.
Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Comp1004: Environments The Java Library. Coming up Recap – Encapsulation – Constructors – Loops – Arrays – ArrayList – Iterators The Java Library – Implementation.
1 Iterator Pattern (A Behavioral Pattern) Prepared by: Neha Tomar.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
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.
IndexedListWithIteratorsViaLinear1Ind
Linked Data Structures
EKT472: Object Oriented Programming
Iterators.
Objects First with Java CITS1001 week 4
Interfaces.
Software Development Iterators
ADT’s, Collections/Generics and Iterators
Functional Processing of Collections (Advanced)
COS 260 DAY 9 Tony Gauvin.
In-Class Extended Example Ch. 6.4
ArraySet Methods and ArrayIterator
CSE 143 Lecture 27: Advanced List Implementation
Iterator.
"First things first, but not necessarily in that order." -Dr. Who
Object Oriented Programming in java
Iteration Abstraction
Example: LinkedSet<T>
Collections James Brucker.
CSE 214 – Computer Science I More on Linked Lists
Interator and Iterable
"First things first, but not necessarily in that order " -Dr. Who
"First things first, but not necessarily in that order " -Dr. Who
slides created by Alyssa Harding
List Interface ArrayList class implements the List Interface
Iterators Dan Fleck.
TCSS 143, Autumn 2004 Lecture Notes
Introduction to Java Collection
Presentation transcript:

"First things first, but not necessarily in that order " -Dr. Who Iterators "First things first, but not necessarily in that order " -Dr. Who

Iterators ArrayList is part of the Java Collections Framework Collection is an interface that specifies the basic operations every collection (data structure) should have Some Collections don’t have a definite order Sets, Maps, Graphs How to access all the items in a Collection with no specified order? Iterators

Iterator Interface An iterator object is a “one shot” object it is designed to go through all the elements of a Collection once if you want to go through the elements of a Collection again you have to get another iterator object Iterators are obtained by calling a method from the Collection Iterators

Iterator Iterface Methods The Iterator interface specifies 3 methods: boolean hasNext() //returns true if this iteration has more elements E next() //returns the next element in this iteration //pre: hasNext() void remove() /*Removes from the underlying collection the last element returned by the iterator. pre: This method can be called only once per call to next. After calling, must call next again before calling remove again. */ Iterators

Iterator Imagine a fence made up of fence posts and rail sections Iterators

Fence Analogy The iterator lives on the fence posts The data in the collection are the rails Iterator created at the far left post As long as a rail exists to the right of the Iterator, hasNext() is true iterator object Iterators

Fence Analogy ArrayList<String> names = new ArrayList<String>(); names.add(“Jan”); names.add(“Levi”); names.add(“Tom”); names.add(“Jose”); Iterator<String> it = names.iterator(); int i = 0; //the i is just to illustrate “Jan” “Levi” “Tom” “Jose” Iterators

Fence Analogy while( it.hasNext() ) { i++; System.out.println( it.next() ); } // when i == 1, prints out Jan first call to next moves iterator to next post and returns “Jan” “Jan” “Levi” “Tom” “Jose” Iterators

Fence Analogy while( it.hasNext() ) { i++; System.out.println( it.next() ); } // when i == 2, prints out Levi “Jan” “Levi” “Tom” “Jose” Iterators

Fence Analogy while( it.hasNext() ) { i++; System.out.println( it.next() ); } // when i == 3, prints out Tom “Jan” “Levi” “Tom” “Jose” Iterators

Fence Analogy while( it.hasNext() ) { i++; System.out.println( it.next() ); } // when i == 4, prints out Jose “Jan” “Levi” “Tom” “Jose” Iterators

Fence Analogy while( it.hasNext() ) { i++; System.out.println( it.next() ); } // call to hasNext returns false // while loop stops “Jan” “Levi” “Tom” “Jose” Iterators

Typical Iterator Pattern public void printAll(Collection<String> list) { Iterator<String> it = list.iterator(); while( it.hasNext() ) { String temp = it.next(); System.out.println( temp ); } Iterators

remove method AnIterator can be used to remove things from the Collection Can only be called once per call to next() public void removeWordsOfLength(int len) { Iterator<String> it = myList.iterator while( it.hasNext() ) { String temp = it.next(); if(temp.length() == len) it.remove(); } // original list = [“dog”, “cat”, “hat”, “sat”] // resulting list after removeWordsOfLength(3) ? Iterators

The Iterable Interface A related interface is Iterable One method in the interface: public Iterator<T> iterator() Why? Anything that implements the Iterable interface can be used in the for each loop. ArrayList<Integer> list; //code to create and fill list int total = 0; for( int x : list ) total += x; Iterators

Iterable If you simply want to go through all the elements of a Collection (or Iterable thing) use the for each loop hides creation of the Iterator public void printAllOfLength(ArrayList<String> names, int len){ //pre: names != null, names only contains Strings //post: print out all elements of names equal in // length to len for(String s : names){ if( s.length() == len ) System.out.println( s ); } Iterators

Comodification If a Collection (ArrayList) is changed while an iteration via an iterator is in progress an Exception will be thrown the next time the next() or remove() methods are called via the iterator ArrayList<String> names = new ArrayList<String>(); names.add(“Jan”); Iterator<String> it = names.iterator(); names.add(“Andy”); it.next(); // exception will occur here Iterators