Transcript while

Grouping objects
Collections and
iterators
Main concepts to be covered
Collections
 Loops
 Iterators

The requirement to group
objects

Many applications involve collections of
objects:
Personal organizers.
 Library catalogs.
 Online shopping.


The number of items to be stored varies.
Items added.
 Items deleted.

A personal notebook
Notes may be stored.
 Individual notes can be viewed.
 There is no limit to the number of notes.
 It will tell how many notes are stored.

import java.util.ArrayList;
/**
* ...
Field declaration
*/
public class Notebook
{
// Storage for an arbitrary number of notes.
private ArrayList<String> notes;
/**
* Perform any initialization required for the
* notebook.
Field initialisation
*/
public Notebook()
{
notes = new ArrayList<String>();
}
...
}
Lecture 4. Grouping objects
5
Pablo Romero, Department of Informatics
Object structures with
collections
<String>
Adding a third note
<String>
Using the collection
public class Notebook
{
private ArrayList<String> notes;
...
public void storeNote(String note)
{
notes.add(note);
Adding a new note
}
public int numberOfNotes()
{
return notes.size();
}
...
}
Returning the number of notes
(delegation).
Index numbering
<String>
Retrieving an object
Index validity checks
public void showNote(int noteNumber)
{
if(noteNumber < 0) {
// This is not a valid note number.
}
else {
if(noteNumber < numberOfNotes()) {
System.out.println(notes.get(noteNumber));
}
else {
// This is not a valid note number.
}
}
}
Retrieve and print the note
Removal may affect numbering
(deleting the second note)
<String>
Review
Collections allow an arbitrary number of
objects to be stored.
 Class libraries (packages) usually contain
tried-and-tested collection classes.
 We have used the ArrayList class from
the java.util package.


To use hash maps we would need to use
the HashMap class
Review
Items may be added and removed.
 Each item has an index.
 Index values may change if items are
removed (or further items added).
 The main ArrayList methods are add,
get, remove and size.

Iteration

We often want to perform some actions an
arbitrary number of times.



E.g., print all the notes in the notebook. How many
are there?
Most programming languages include loop
statements to make this possible.
Three sorts of loop statement.



for-each loop
while loop
for loop
For-each loop pseudo code
General form of a for-each loop
for keyword
Loop variable
Our collection
for(ElementType element: collection) {
loop body
Statements to be repeated
}
Pseudo-code example to print every note
for each(note in the notes collection) {
show the next note
}
A Java example
/**
* List all notes in the notebook.
*/
public void listNotes()
{
for(String note : notes) {
System.out.println(note);
}
}
Loop variable
How many times is this executed?
While loop pseudo code
General form of a while loop
while keyword
Boolean test
while(loop condition) {
loop body
}
Statements to be repeated
Pseudo-code example to print every note
while(there is at least one more note to be printed) {
show the next note
}
A Java example
/**
* List all notes in the notebook.
*/
public void listNotes()
Local index variable
{
int index = 0;
while(index < notes.size()) {
System.out.println(notes.get(index));
index++;
}
}
Increment by one
Exercise
Write the method noteExists(String
searchString) that searches the notebook for a
specific string and returns true if any of the
strings in the notebook contains the parameter
and false otherwise. You might need to use a
variable of type boolean (which can take the
literal values true or false) and the contains
method from the String class which takes a
parameter of type string and returns true if the
parameter is included in the string and false
otherwise.
For loop pseudo-code
General form of a for loop
for(initialization; condition; post-body action) {
statements to be repeated
}
Equivalent in while-loop form
initialization;
while(condition) {
statements to be repeated
post-body action
}
while loop version
public void listNotes()
{
int index = 0;
while(index < notes.size()) {
System.out.println(notes.get(index));
index++;
}
}
for loop version
for(int index = 0; index < notes.size(); index++) {
System.out.println(notes.get(index));
}
Lecture 4. Grouping objects
21
Pablo Romero, Department of Informatics
Three types of loops
for-each if you need to iterate over all
elements of a collection
 while or for if the iteration is not related to
a collection

for when the number of iterations is known
 while when number of iterations is
determined on the fly

Iterators
while loop made simple
 Special classes to iterate over collections
 Have methods to

Check whether there are more elements
 Obtain the next element

Iterating over a collection
java.util.Iterator
Returns an Iterator
object
Iterator<ElementType> it = myCollection.iterator();
while(it.hasNext()) {
call it.next() to get the next object
do something with that object
}
public void listNotes()
{
Iterator<String> it = notes.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
Review


Loop statements allow a block of statements to
be repeated.
Three types of loops




for-each
while
for
Collection classes have special Iterator
objects that simplify iteration over the whole
collection.