Presentation is loading. Please wait.

Presentation is loading. Please wait.

Grouping Objects 3 Iterators, collections and the while loop.

Similar presentations


Presentation on theme: "Grouping Objects 3 Iterators, collections and the while loop."— Presentation transcript:

1 Grouping Objects 3 Iterators, collections and the while loop

2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Iterators The for-each loop is a simple way of iterating through every element of a collection. The while loop gives a way of iterating through some elements of a collection – it lets us stop when we’ve had enough (e.g. because we found something we were looking for). In the while loopsseen before, we had to initialise an index variable (e.g. to 0 ) into the collection, use it (to get elements from the collection) and maintain (e.g. increment) it within the while loop. In the while loops seen before, we had to initialise an index variable (e.g. to 0 ) into the collection, use it (to get elements from the collection) and maintain (e.g. increment) it within the while loop. Iterators – in conjunction with a while loop –simplify (partial) iteration through a collection Iterators – in conjunction with a while loop – simplify (partial) iteration through a collection.

3 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling while the value of index is less than the size of the collection, do print the note at position index and, then, increment index. /** * List all notes in the notebook. * List all notes in the notebook. */ */ public void listNotes() { int index = 0; int index = 0; while (index < notes.size()) { while (index < notes.size()) { System.out.println (notes.get(index)); System.out.println (notes.get(index)); index++; index++; }} Pseudo-code expression of the actions of the above while loop (Increment index by 1) index = index + 1 private ArrayList notes; // The notebook... other fields, constructor(s), other methods previous example

4 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling while the iterator says there is another element, do ask the iterator for the next element and print it. /** * List all notes in the notebook. * List all notes in the notebook. */ */ public void listNotes() { Iterator it = notes.iterator(); Iterator it = notes.iterator(); while (it.hasNext()) { while (it.hasNext()) { System.out.println (it.next()); } System.out.println (it.next()); }} Pseudo-code expression of the actions of the above while loop private ArrayList notes; // The notebook... other fields, constructor(s), other methods Every collection object has an Iterator object

5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes.add("...")notes.add("...") notes.add("...")notes.add("...") notes.add("...")notes.add("...") notes.add("...")notes.add("...") notes:ArrayList :String

6 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String it:Iterator Iterator it = notes.iterator() : String

7 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String it:Iterator it.hasNext()it.hasNext() : String ✔

8 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String it:Iterator : String System.out.println (it.next());

9 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String it:Iterator : String String tmp = it.next(); String tmp = it.next(); System.out.println (tmp); System.out.println (tmp); String tmp = it.next(); String tmp = it.next(); System.out.println (tmp); System.out.println (tmp);

10 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String it:Iterator : String String tmp = it.next(); String tmp = it.next(); System.out.println (tmp); System.out.println (tmp); String tmp = it.next(); String tmp = it.next(); System.out.println (tmp); System.out.println (tmp); tmp

11 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String it:Iterator : String tmp it.hasNext()it.hasNext() ✔

12 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String it:Iterator : String tmp String tmp = it.next(); String tmp = it.next(); System.out.println (tmp); System.out.println (tmp); String tmp = it.next(); String tmp = it.next(); System.out.println (tmp); System.out.println (tmp);

13 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String it:Iterator : String String tmp = it.next(); String tmp = it.next(); System.out.println (tmp); System.out.println (tmp); String tmp = it.next(); String tmp = it.next(); System.out.println (tmp); System.out.println (tmp); tmp

14 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String it.hasNext()it.hasNext() ✔ it:Iterator tmp

15 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String String tmp = it.next(); String tmp = it.next(); System.out.println (tmp); System.out.println (tmp); String tmp = it.next(); String tmp = it.next(); System.out.println (tmp); System.out.println (tmp); it:Iterator tmp

16 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String it:Iterator : String String tmp = it.next(); String tmp = it.next(); System.out.println (tmp); System.out.println (tmp); String tmp = it.next(); String tmp = it.next(); System.out.println (tmp); System.out.println (tmp); tmp

17 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String it.hasNext()it.hasNext() ✔ it:Iterator tmp

18 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String String tmp = it.next(); String tmp = it.next(); System.out.println (tmp); System.out.println (tmp); String tmp = it.next(); String tmp = it.next(); System.out.println (tmp); System.out.println (tmp); it:Iterator tmp

19 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String String tmp = it.next(); String tmp = it.next(); System.out.println (tmp); System.out.println (tmp); String tmp = it.next(); String tmp = it.next(); System.out.println (tmp); System.out.println (tmp); tmp it:Iterator

20 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String tmp it:Iterator it.hasNext()it.hasNext() ✗

21 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String Iterator it = notes.iterator(); while (it.hasNext()) { System.out.println (it.next()); } System.out.println (it.next()); } Reprise...

22 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Iterator it = notes.iterator(); while (it.hasNext()) { System.out.println (it.next()); } System.out.println (it.next()); } notes:ArrayList :String it:Iterator Reprise...

23 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String Iterator it = notes.iterator(); while (it.hasNext()) { System.out.println (it.next()); } System.out.println (it.next()); } it:Iterator ✔ Reprise...

24 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String Iterator it = notes.iterator(); while (it.hasNext()) { System.out.println (it.next()); } System.out.println (it.next()); } it:Iterator Reprise...

25 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String Iterator it = notes.iterator(); while (it.hasNext()) { System.out.println (it.next()); } System.out.println (it.next()); } it:Iterator ✔ Reprise...

26 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String Iterator it = notes.iterator(); while (it.hasNext()) { System.out.println (it.next()); } System.out.println (it.next()); } it:Iterator Reprise...

27 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String Iterator it = notes.iterator(); while (it.hasNext()) { System.out.println (it.next()); } System.out.println (it.next()); } ✔ it:Iterator Reprise...

28 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String Iterator it = notes.iterator(); while (it.hasNext()) { System.out.println (it.next()); } System.out.println (it.next()); } it:Iterator Reprise...

29 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String Iterator it = notes.iterator(); while (it.hasNext()) { System.out.println (it.next()); } System.out.println (it.next()); } it:Iterator ✔ Reprise...

30 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String Iterator it = notes.iterator(); while (it.hasNext()) { System.out.println (it.next()); } System.out.println (it.next()); } it:Iterator Reprise...

31 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling ✗ notes:ArrayList :String Iterator it = notes.iterator(); while (it.hasNext()) { System.out.println (it.next()); } System.out.println (it.next()); } it:Iterator Reprise...

32 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList :String Iterator it = notes.iterator(); while (it.hasNext()) { System.out.println (it.next()); } System.out.println (it.next()); } it:Iterator Reprise...

33 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling boolean searching = true; Iterator it = myCollection.iterator(); while (searching && it.hasNext()) { ElementType thing = it.next();... do something with thing... maybe set searching false } Iterator-while loop pattern while we’re still searching and there is more left to search, do consider the next thing in the collection and whether to continue. construct iterator early exit condition Statements to be repeated Pseudo-code expression of the actions of this while loop

34 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Searching a Collection /** * Search all notes in the notebook for the first * one containing a specified piece of text. * * @param searchString The sought text * @return The note containing the text * (if none found, return null) * Search all notes in the notebook for the first * one containing a specified piece of text. * * @param searchString The sought text * @return The note containing the text * (if none found, return null) */ */ public String searchNotes2 (String searchString) {... code body of the method... code body of the method} If null is returned, it means no note containing the search string was found.

35 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public String searchNotes2 (String searchString) { String found = null; // so far, not found it String found = null; // so far, not found it Iterator it = notes.iterator(); Iterator it = notes.iterator(); while ((found == null) && (it.hasNext())) { while ((found == null) && (it.hasNext())) { String note = it.next(); String note = it.next(); if (note.contains(searchString)) { if (note.contains(searchString)) { // stop here - we don't need to keep looking. // stop here - we don't need to keep looking. found = note; found = note; } } return found; } return found;} Either we found a note containing the search string... and stopped searching … Or we searched the whole collection, didn’t find it and found is still null.

36 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Either we found a note containing the search string... returned it … exiting the loop and the method immediately. Or we searched the whole collection, didn’t find it and returned null. alternative coding 1 public String searchNotes2 (String searchString) { Iterator it = notes.iterator(); Iterator it = notes.iterator(); while (it.hasNext()) { while (it.hasNext()) { String note = it.next(); String note = it.next(); if (note.contains(searchString)) { if (note.contains(searchString)) { // stop here - we don't need to keep looking. // stop here - we don't need to keep looking. return note; return note; } } return null; } return null;}

37 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Either we found a note containing the search string... returned it … exiting the loop and the method immediately. Or we searched the whole collection, didn’t find it and returned null. alternative coding 2 public String searchNotes2 (String searchString) { for (String note : notes) { for (String note : notes) { if (note.contains(searchString)) { if (note.contains(searchString)) { // stop here - we don't need to keep looking. // stop here - we don't need to keep looking. return note; return note; } } return null; } return null;}

38 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Different Forms of Iteration Ways to iterate over a collection: Ways to iterate over a collection: for-each loopfor-each loop Use if we want to process every element.Use if we want to process every element. But we can exit early with a return statement. But we can exit early with a return statement. Often used with a collection where indexed access (e.g. get(index) ) is inefficient … or impossible. Often used with a collection where indexed access (e.g. get(index) ) is inefficient … or impossible. while loopwhile loop Need to declare and manage an index variable.Need to declare and manage an index variable. Need not involve a collection.Need not involve a collection. Iterator object + while loopIterator object + while loop Need to declare and manage an iterator variable.Need to declare and manage an iterator variable. Often used with a collection where indexed access (e.g. get(index) ) is inefficient … or impossible. Often used with a collection where indexed access (e.g. get(index) ) is inefficient … or impossible. Iteration is an fundamental programming pattern. Iteration is an fundamental programming pattern.

39 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Looking a little deeper: Looking a little deeper: for-each loopfor-each loop Use if we want to process every element.Use if we want to process every element. But we can exit early with a return statement. But we can exit early with a return statement. Often used with a collection where indexed access (e.g. get(index) ) is inefficient … or impossible. Often used with a collection where indexed access (e.g. get(index) ) is inefficient … or impossible. Iterator object + while loopIterator object + while loop Need to declare and manage an iterator variable.Need to declare and manage an iterator variable. Often used with a collection where indexed access (e.g. get(index) ) is inefficient … or impossible. Often used with a collection where indexed access (e.g. get(index) ) is inefficient … or impossible. For the examples shown, it has always been simpler to code with a for-each loop, rather than an iterator-while loop. Different Forms of Iteration

40 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Looking a little deeper: Looking a little deeper: for-each loopfor-each loop Use if we want to process every element.Use if we want to process every element. But we can exit early with a return statement. But we can exit early with a return statement. Often used with a collection where indexed access (e.g. get(index) ) is inefficient … or impossible. Often used with a collection where indexed access (e.g. get(index) ) is inefficient … or impossible. Iterator object + while loopIterator object + while loop Need to declare and manage an iterator variable.Need to declare and manage an iterator variable. Often used with a collection where indexed access (e.g. get(index) ) is inefficient … or impossible. Often used with a collection where indexed access (e.g. get(index) ) is inefficient … or impossible. For the examples shown, it has always been simpler to code with a for-each loop, rather than an iterator-while loop. With an Iterator, however, we can remove elements from a collection – we can’t do that with a for-each loop. Different Forms of Iteration

41 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling With an Iterator, however, we can remove elements from a collection – we can’t do that with a for-each loop. Looking a little deeper: Looking a little deeper: for-each loopfor-each loop Use if we want to process every element.Use if we want to process every element. But we can exit early with a return statement. But we can exit early with a return statement. Often used with a collection where indexed access (e.g. get(index) ) is inefficient … or impossible. Often used with a collection where indexed access (e.g. get(index) ) is inefficient … or impossible. Iterator object + while loopIterator object + while loop Need to declare and manage an iterator variable.Need to declare and manage an iterator variable. Often used with a collection where indexed access (e.g. get(index) ) is inefficient … or impossible. Often used with a collection where indexed access (e.g. get(index) ) is inefficient … or impossible. If we need to iterate through two (or more) collection objects in the same loop, we have to use a two (or more) iterator-while loop. Different Forms of Iteration

42 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling The auction Project The auction project provides further illustration of collections and iteration. null One further point to follow up is the null value: there is no objectUsed to indicate: there is no object. null We can test if an object variable holds the null value: null We can make a object variable hold the null value: if (x == null) {...} x = null;

43 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Review Loop statements allow a block of statements to be repeated. Loop statements allow a block of statements to be repeated. The for-each loop allows iteration over a whole collection – but you can exit early with a return. The for-each loop allows iteration over a whole collection – but you can exit early with a return. The while loop allows the repetition to be controlled by a boolean expression. The while loop allows the repetition to be controlled by a boolean expression. A special Iterator object can be obtained from any collection object: A special Iterator object can be obtained from any collection object: Used with a while loop, this provides sequential iteration through the whole (or initial part of the) collection.Used with a while loop, this provides sequential iteration through the whole (or initial part of the) collection.


Download ppt "Grouping Objects 3 Iterators, collections and the while loop."

Similar presentations


Ads by Google