Presentation is loading. Please wait.

Presentation is loading. Please wait.

Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)

Similar presentations


Presentation on theme: "Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)"— Presentation transcript:

1 Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)

2

3 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to be covered Testing for equality Iterators

4 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Identity vs Equality 1 if (person1 == person2) {... } “Fred” :Person “Jill” :Person person1person2 Are they the same Person? No!No!

5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Identity vs Equality 2 if (person1 == person2) {... } “Fred” :Person “Fred” :Person person1person2 Are they the same Person? No!No!

6 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Identity vs Equality 3 if (person1 == person2) {... } “Fred” :Person person1person2 Are they the same Person? Yes!Yes!

7 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Are they the same String? “bye” :String “bye” :String input This String object is constructed for the string literal in the code – there is no named variable pointing at it. No!No! Identity vs Equality 4 (Strings) if (input == "bye") {... } String input = reader.getInput(); if (input == "bye") {... }

8 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling “bye” :String “bye” :String input This String object is constructed for the string literal in the code – there is no named variable pointing at it. Identity vs Equality 5 (Strings) Do they have the same characters? Yes!Yes! if (input.equals("bye")) {... } String input = reader.getInput(); if (input.equals("bye")) {... }

9 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling String equality Strings should always be compared with.equals(…) Are they the same String? Do they have the same characters? if (input == "bye") {... } if (input.equals("bye")) {... } String input = reader.getInput(); if (input == "bye") {... } if (input.equals("bye")) {... }

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

11 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. */ public void listNotes() { int index = 0; while (index < notes.size()) { System.out.println (notes.get(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

12 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. */ public void listNotes() { Iterator it = notes.iterator(); while (it.hasNext()) { 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

13 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

14 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

15 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 ✔

16 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());

17 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);

18 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

19 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() ✔

20 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);

21 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

22 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

23 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

24 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

25 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

26 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

27 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

28 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() ✗

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()); } Reprise...

30 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()); } notes:ArrayList :String 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()); } 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()); } it:Iterator Reprise...

33 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()); } it:Iterator ✔ Reprise...

34 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()); } it:Iterator Reprise...

35 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()); } ✔ it:Iterator Reprise...

36 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()); } it:Iterator Reprise...

37 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()); } it:Iterator ✔ Reprise...

38 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()); } it:Iterator Reprise...

39 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()); } it:Iterator Reprise...

40 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()); } it:Iterator Reprise...

41 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

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

43 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(); while ((found == null) && ( 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.

44 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(); while ( 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;}

45 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;}

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

47 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Different forms of iteration 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.

48 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Different forms of iteration 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.

49 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. Different forms of iteration 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.

50 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;

51 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 Iterators, collections and the while loop Equality and Equality == and equals(…)"

Similar presentations


Ads by Google