Presentation is loading. Please wait.

Presentation is loading. Please wait.

Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.

Similar presentations


Presentation on theme: "Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators."— Presentation transcript:

1 Grouping objects Collections and iterators

2 Main concepts to be covered Collections Loops Iterators

3 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.

4 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.

5 Lecture 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 notes; /** * Perform any initialization required for the * notebook. */ public Notebook() { notes = new ArrayList (); }... } Field declaration Field initialisation

6 Object structures with collections

7 Adding a third note

8 Using the collection public class Notebook { private ArrayList 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).

9 Index numbering

10 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. }

11 Removal may affect numbering (deleting the second note)

12 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

13 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.

14 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

15 For-each loop pseudo code for(ElementType element: collection) { loop body } Loop variable for 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

16 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?

17 While loop pseudo code while(loop condition) { loop body } Boolean test while 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

18 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

19 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.

20 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 }

21 Lecture 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++; }

22 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

23 Iterators while loop made simple Special classes to iterate over collections Have methods to Check whether there are more elements Obtain the next element

24 Iterating over a collection Iterator 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 it = notes.iterator(); while(it.hasNext()) { System.out.println(it.next()); }

25 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.


Download ppt "Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators."

Similar presentations


Ads by Google