grouping objects collections and iterators. main concepts to be covered collections loops iterators
TRANSCRIPT
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.
Lecture 4. Grouping objects 5 Pablo Romero, Department of InformaticsLecture 4. Grouping objects 5 Pablo Romero, Department of Informatics
import java.util.ArrayList;
/** * ... */public class Notebook{ // Storage for an arbitrary number of notes. private ArrayList<String> notes; /** * Perform any initialization required for the * notebook. */ public Notebook() { notes = new ArrayList<String>(); }
...}
Field declaration
Field initialisation
Object structures with collections
<String>
Adding a third note
<String>
Using the collectionpublic class Notebook{ private ArrayList<String> notes; ... public void storeNote(String note) { notes.add(note); } public int numberOfNotes() { return notes.size(); }
...}
Adding a new note
Returning the number of notes(delegation).
Index numbering
<String>
Index validity checks
Retrieving an object
Retrieve and print the note
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. } }}
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
for(ElementType element: collection) { loop body}
Loop variablefor keyword
Statements to be repeated
General form of a for-each loop
for each(note in the notes collection) { show the next note}
Pseudo-code example to print every note
Our collection
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
while(loop condition) { loop body}
Boolean testwhile keyword
Statements to be repeated
General form of a while loop
while(there is at least one more note to be printed) { show the next note}
Pseudo-code example to print every note
A Java example
/** * List all notes in the notebook. */public void listNotes(){ int index = 0; while(index < notes.size()) { System.out.println(notes.get(index)); index++; }}
Increment by one
Local index variable
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
for(initialization; condition; post-body action) { statements to be repeated}
General form of a for loop
Equivalent in while-loop form
initialization;while(condition) { statements to be repeated post-body action}
Lecture 4. Grouping objects 21 Pablo Romero, Department of InformaticsLecture 4. Grouping objects 21 Pablo Romero, Department of Informatics
for(int index = 0; index < notes.size(); index++) { System.out.println(notes.get(index));}
for loop version
while loop version
public void listNotes(){ int index = 0; while(index < notes.size()) { System.out.println(notes.get(index)); index++; }}
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
Iterator<ElementType> 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 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.