CPSC 2231_Chapter 4_Week 3
CPSC 2231_Chapter 4_Week 3
Programming Workshop
Fall, 2024
1
Review: For loop vs For-each loop
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
3
Grouping objects
Iterator objects
4
Iterator type
5
Iterator vs iterator()
6
Using an Iterator object
returns an Iterator object
java.util.Iterator
Iterator<ElementType> it = myCollection.iterator();
while(it.hasNext()) {
call it.next() to get the next object
do something with that object
}
9
myList:List
myList.iterator()
:Iterator
10
myList:List
:Iterator:Iterator
hasNext()?
next()
Element e = iterator.next();
11
myList:List
:Iterator :Iterator
hasNext()?
next()
12
myList:List
:Iterator :Iterator
hasNext()?
next()
13
myList:List
:Iterator :Iterator
hasNext()?
next()
14
myList:List
:Iterator
hasNext()? ✗
15
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.
16
Removing from a collection
Iterator<Track> it = tracks.iterator();
while(it.hasNext()) {
Track t = it.next();
String artist = t.getArtist();
if(artist.equals(artistToRemove)) {
it.remove();
}
}
17
Removing from a collection –
wrong!
int index = 0;
while(index < tracks.size()) {
Track t = tracks.get(index);
String artist = t.getArtist();
if(artist.equals(artistToRemove)) {
tracks.remove(index);
}
index++;
}
Can you spot what is wrong?
18
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.
19
The auction project
• The auction project provides further
illustration of collections and iteration.
• Examples of using null.
• Anonymous objects.
• Chaining method calls.
20
The auction project
21
auction project
Online Auction system:
•Sell items via enterLot with String description
•Auction object creates Lot object for entered lot
• lot number and description is assigned with no bidders
•Bidder Person can register with only their name
•Place bid with bidFor method of Auction object
with lot number and how much to bid
• Lot number passed so Lot objects internal to Auction
•Auction object transforms bid amount to object
•Lot records the highest bid object
© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. 22
null
if(highestBid == null) …
23
Anonymous objects
• Objects are often created and handed on
elsewhere immediately:
lots.add(new Lot(…));
24
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()
25
Chaining method calls
• Each method in the chain is called on the
object returned from the previous method call
in the chain.
String name =
lot.getHighestBid().getBidder().getName();
26
Review
• Collections are used widely in many different
applications.
• The Java library provides many different
‘ready made’ collection classes.
• Collections are often manipulated using
iterative control structures.
• The while loop is the most important control
structure to master.
27
Review
• Some collections lend themselves to index-
based access; e.g. ArrayList.
• Iterator provides a versatile means to
iterate over different types of collection.
• Removal using an Iterator is less error-
prone in some circumstance.
28
Midterm Exam
• Midterm exam is scheduled on 10/17, Thursday.
29
Thank you
30