grouping objects collections and iterators. main concepts to be covered collections loops iterators
TRANSCRIPT
![Page 1: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/1.jpg)
Grouping objects
Collections and iterators
![Page 2: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/2.jpg)
Main concepts to be covered
Collections Loops Iterators
![Page 3: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/3.jpg)
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.
![Page 4: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/4.jpg)
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.
![Page 5: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/5.jpg)
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
![Page 6: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/6.jpg)
Object structures with collections
<String>
![Page 7: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/7.jpg)
Adding a third note
<String>
![Page 8: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/8.jpg)
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).
![Page 9: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/9.jpg)
Index numbering
<String>
![Page 10: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/10.jpg)
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. } }}
![Page 11: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/11.jpg)
Removal may affect numbering (deleting the second note)
<String>
![Page 12: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/12.jpg)
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
![Page 13: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/13.jpg)
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.
![Page 14: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/14.jpg)
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
![Page 15: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/15.jpg)
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
![Page 16: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/16.jpg)
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?
![Page 17: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/17.jpg)
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
![Page 18: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/18.jpg)
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
![Page 19: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/19.jpg)
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.
![Page 20: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/20.jpg)
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}
![Page 21: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/21.jpg)
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++; }}
![Page 22: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/22.jpg)
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
![Page 23: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/23.jpg)
Iterators
while loop made simple Special classes to iterate over collections Have methods to
Check whether there are more elements Obtain the next element
![Page 24: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/24.jpg)
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()); }
![Page 25: Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649e545503460f94b4b5c7/html5/thumbnails/25.jpg)
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.