Grouping objects Iterators. Iterator and iterator() Collections have an iterator() method. This returns an Iterator object. Iterator has three methods:

Slides:



Advertisements
Similar presentations
Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Advertisements

Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
1 Features of the collection It increases its capacity as necessary. It keeps a private count: –size() accessor. It keeps the objects in order. Details.
Grouping objects Introduction to collections 5.0.
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
Using Collections. Review of Collections Using an ArrayList It increases its capacity as necessary. It keeps a private count ( size() accessor). It keeps.
Grouping Objects 3 Iterators, collections and the while loop.
String Concatenation (operator overloading) 3.0.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Understanding class definitions Looking inside classes 3.0.
More sophisticated behavior Using library classes to implement some more advanced functionality 4.0.
Generic Classes and 'for each' Loops List zoo = new ArrayList (); // … add several Animals to the zoo for (Animal animal : zoo) // do something with animal.
Grouping Objects 2 Collections and the for-each loop Collections and the while loop.
Grouping Objects 1 Introduction to Collections.
Grouping objects Collections and iterators. 04/11/2004Lecture 4: Grouping Objects2 Main concepts to be covered Collections Loops Iterators Arrays.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
More sophisticated behavior Using library classes to implement some more advanced functionality 3.0.
Modul 3 Collections af objekter Arraylist Collections Objektorienteret design og Java. 4.0.
Programming with Collections Grouping & Looping - Collections and Iteration Week 7.
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
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
Grouping objects Arrays, Collections and Iterators 1.0.
OBJECT INTERACTION CITS1001. Overview Coupling and Cohesion Internal/external method calls null objects Chaining method calls Class constants Class variables.
IOOP Review 5.0. Topics (1) Bits and Bytes Classes and Objects Primitive vs. Object Types Java Operators and Expressions Keyword this null vs. void Enumerated.
Grouping objects Introduction to collections 5.0.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
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)
Understanding class definitions
Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 1.0.
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.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
Grouping objects Arrays. 2 Fixed-size collections The maximum collection size may be pre-determined with an upper limit Array is an fixed-size collection.
Grouping objects Introduction to collections 5.0.
1 COS 260 DAY 7 Tony Gauvin. 2 Agenda Questions? 3 rd Mini quiz next class (September 28) –Chap 3 concepts Assignment 1 Corrected Assignment 2 posted.
Comparing ArrayLists and Arrays. ArrayLists ArrayLists are one type of flexible-size collection classes supported by Java –ArrayLists increase and decrease.
1 COS 260 DAY 9 Tony Gauvin. 2 Agenda Questions? 3 rd Mini quiz –Good results ->All A’s –Threw out question on name overloading 4 th Mini quiz next class.
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)
Grouping objects Introduction to collections 6.0.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
Coming up ArrayList ArrayList vs Array – Declaration – Insertion – Access – Removal Wrapper classes Iterator object.
for-each PROS CONS easy to use access to ALL items one-by-one
Objects First with Java CITS1001 week 4
Objects First with Java Introduction to collections
Fixed-sized collections
COS 260 DAY 7 Tony Gauvin.
Object INTERACTION CITS1001 week 3.
Functional Processing of Collections (Advanced)
Objects First with Java CITS1001
COS 260 DAY 9 Tony Gauvin.
4 Grouping objects (cont.)
Objects First with Java Introduction to collections
COS 260 DAY 13 Tony Gauvin.
Alice in Action with Java
"First things first, but not necessarily in that order " -Dr. Who
Iterator.
COS 260 DAY 8 Tony Gauvin.
COS 260 DAY 8 Tony Gauvin.
Objects First with Java Introduction to collections
Exercise 1 Declare a constant of type int called SIZE and initialize it to value 10 Declare two int arrays of size “SIZE” Assume that those int arrays.
COS 260 DAY 11 Tony Gauvin.
Collections and iterators
Improving structure with inheritance
Collections and iterators
Presentation transcript:

Grouping objects Iterators

Iterator and iterator() Collections have an iterator() method. This returns an Iterator object. Iterator has three methods: –boolean hasNext() –E next() –void remove()

3 Using an Iterator object Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Iterator it = myCollection.iterator(); while(it.hasNext()) { call it.next() to get the next object do something with that object } java.util.Iterator returns an Iterator object public void listAllFiles() { Iterator it = files.iterator(); while(it.hasNext()) { Track tk = it.next(); System.out.println(tk.getDetails()); }

4 Iterator mechanics Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

5 :Element myList:List :Element :Iterator myList.iterator() :Element

6 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling :Element :Iterator hasNext()? ✔ next() Element e = iterator.next(); :Element :Iterator myList:List

7 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling :Element hasNext()? ✔ next() :Element :Iterator myList:List

8 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling :Element hasNext()? ✔ next() :Element :Iterator myList:List

9 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling :Element hasNext()? ✔ next() :Element :Iterator myList:List

10 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling :Element hasNext()? ✗ :Element :Iterator myList:List

11 Index versus Iterator Ways to iterate over a collection: –for-each loop. Use if we want to process every element. –while loop. Use if we might want to stop part way through. Use for repetition that doesn't involve a collection. –Iterator object. Use if we might want to stop part way through. Often used with collections where indexed access is not very efficient, or impossible. Use to remove from a collection. Iteration is an important programming pattern. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

12 Removing from a collection Iterator it = tracks.iterator(); while(it.hasNext()) { Track t = it.next(); String artist = t.getArtist(); if(artist.equals(artistToRemove)) { it.remove(); } Use the Iterator ’s remove method.

13 Review Loop statements allow a block of statements to be repeated. The for-each loop allows iteration over a whole collection. The while loop allows the repetition to be controlled by a boolean expression. All collection classes provide special Iterator objects that provide sequential access to a whole collection. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

14 The auction project The auction project provides further illustration of collections and iteration. Examples of using null. Anonymous objects. Chaining method calls. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

15 The auction project

16 null Used with object types. Used to indicate, 'no object'. We can test if an object variable holds the null value: if(highestBid == null) … Used to indicate ‘no bid yet’.

17 Anonymous objects Objects are often created and handed on elsewhere immediately: Lot furtherLot = new Lot(…); lots.add(furtherLot); We don’t really need furtherLot : lots.add(new Lot(…));

18 Chaining method calls Methods often return objects. We often immediately call a method on the returned object. Bid bid = lot.getHighestBid(); Person bidder = bid.getBidder(); We can use the anonymous object concept and chain method calls: lot.getHighestBid().getBidder()

19 Chaining method calls String name = lot.getHighestBid().getBidder().getName(); Each method in the chain is called on the object returned from the previous method call in the chain. Returns a Bid object from the Lot Returns a Person object from the Bid Returns a String object from the Person