CollectionsForOOSE

Download Report

Transcript CollectionsForOOSE

Objects First With Java
A Practical Introduction Using BlueJ
Grouping objects
Collections and iterators
1.0
Main concepts to be covered
•
•
•
•
Collections
Loops (gdb: I dropped this part)
Iterators
Arrays (gdb: I dropped this, too)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
2
The requirement to group
objects
• Many applications involve collections of
objects:
– Personal organizers.
– Library catalogs.
– Student-record system.
• The number of items to be stored varies.
– Items added.
– Items deleted.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
3
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.
• Explore the notebook1 project.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
4
Class libraries
• Collections of useful classes.
• We don’t have to write everything
from scratch.
• Java calls its libraries, packages.
• Grouping objects is a recurring
requirement.
– The java.util package contains
classes for doing this.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
5
import java.util.ArrayList;
/**
* ...
*/
public class Notebook
{
// Storage for an arbitrary number of notes.
private ArrayList notes;
/**
* Perform any initialization required for the
* notebook.
*/
public Notebook()
{
notes = new ArrayList();
}
...
}
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
6
Object structures with
collections
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
7
Adding a third note
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
8
Features of the collection
• It increases its capacity as necessary.
• It keeps a private count (size()
accessor).
• It keeps the objects in order.
• Details of how all this is done are
hidden.
– Does that matter? Does not knowing how
prevent us from using it?
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
9
Using the collection
public class Notebook
{
private ArrayList 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).
...
}
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
10
Index numbering
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
11
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
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
12
Removal may affect
numbering
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
13
Review
• Collections allow an arbitrary number
of objects to be stored.
• Class libraries usually contain triedand-tested collection classes.
• Java’s class libraries are called
packages.
• We have used the ArrayList class
from the java.util package.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
14
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.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
15
Iterating over a collection
java.util.Iterator
Returns an Iterator
object
Iterator it = myCollection.iterator();
while(it.hasNext()) {
call it.next() to get the next object
do something with that object
}
public void listNotes()
{
Iterator it = notes.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
16
The auction project
• The auction project provides further
illustration of collections and
iteration.
• Two further points to follow up:
– The null value.
– Casting. Used to store the result of get
into a variable:
• String message = (String) notes.get(0);
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
17
Review
• Loop statements allow a block of
statements to be repeated.
• A Java while loop allows the
repetition to be controlled by a
boolean expression.
• Collection classes have special
Iterator objects that simplify
iteration over the whole collection.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
18