Presentation is loading. Please wait.

Presentation is loading. Please wait.

Grouping Objects 2 Collections and the for-each loop Collections and the while loop.

Similar presentations


Presentation on theme: "Grouping Objects 2 Collections and the for-each loop Collections and the while loop."— Presentation transcript:

1 Grouping Objects 2 Collections and the for-each loop Collections and the while loop

2

3 Main concepts to be covered (Collections: the ArrayList) Iteration: the for-each loop Iteration: the while loop

4 We often want to perform some actions an arbitrary number of times. We often want to perform some actions an arbitrary number of times. Most programming languages include loop statements to make this possible.Most programming languages include loop statements to make this possible. Java has several kinds of loop statement.Java has several kinds of loop statement. For example, print all the notes in the notebook. But how many are there? We will start with its for-each loop. Iteration

5 We often want to repeat the same action on each object in a collection.We often want to repeat the same action on each object in a collection. The for-each loop provides a simple way to do this.The for-each loop provides a simple way to do this. Iteration (for-each)

6 The for-each Loop for (ElementType element : collection) {... loop body... loop body} for keyword Statement(s) to be repeated General form of the for-each loop loop header For each element in collection, do the things in the loop body. Pseudo-code expression of the actions of a for-each loop

7 /** * List all notes in the notebook. * List all notes in the notebook. */ */ public void listNotes() { for (String note : notes) { for (String note : notes) { System.out.println (note); System.out.println (note); }} for each note in notes, print out note. A Method in NoteBook Must be an object of a collection class – such as ArrayList Must be an object of a collection class – such as ArrayList Pseudo-code expression of the actions of the above for-each loop

8 Review So, loop statements enable a block of statements to be repeated. The for-each loop enables iteration over each element from a collection.

9

10 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling The while Loop A for-each loop repeats the loop body for each object in a collection. A for-each loop repeats the loop body for each object in a collection. Sometimes we require more variation than this. Sometimes we require more variation than this. We can use a boolean condition to decide whether or not to keep going. We can use a boolean condition to decide whether or not to keep going. A while loop provides this control. A while loop provides this control.

11 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling while (loop condition) {... loop body... loop body} while the loop condition is true, do the things in the loop body. boolean test while keyword Statements to be repeated Pseudo-code expression of the actions of a while loop General form of a while loop The while Loop

12 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling /** * 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++; }} while the value of index is less than the size of the collection, do print the note at position index and, then, increment index. Pseudo-code expression of the actions of the above while loop (Increment index by 1) index = index + 1 A Method in NoteBook alternative coding

13 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling /** * List all notes in the notebook. * List all notes in the notebook. */ */ public void listNotes() { for (String note : notes) { for (String note : notes) { System.out.println (note); System.out.println (note); }} for each note in notes, print out note. A Method in NoteBook Pseudo-code expression of the actions of the above for-each loop previous coding

14 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling for-each versus while for-each: for-each: can only be used for loops involving a collectioncan only be used for loops involving a collection easier to writeeasier to write safer: it is guaranteed to stopsafer: it is guaranteed to stop actually, we don’t have to process the whole collection (we’ll show this presently)actually, we don’t have to process the whole collection (we’ll show this presently) while: while: can be used for loops that do not involve a collectioncan be used for loops that do not involve a collection take care: the loop may never end!take care: the loop may never end! used on a collection class, we don’t have to process all of it (we’ll show this presently)used on a collection class, we don’t have to process all of it (we’ll show this presently)

15 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling while loop (not on a collection) int index = 0; while (index <= 42) { System.out.println(index); System.out.println(index); index = index + 2; index = index + 2;} int index = 0; while (index >= 42) { System.out.println(index); System.out.println(index); index = index + 2; index = index + 2;} int index = 0; while (index <= 42) { System.out.println(index); System.out.println(index); index = index - 2; index = index - 2;} // Print all even numbers from 0 to 42 inclusive. // Programming error? This does nothing at all! // Programming error? This loop never ends!

16 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 index of the note containing the text * (if none found, return -1) * Search all notes in the notebook for the first * one containing a specified piece of text. * * @param searchString The sought text * @return The index of the note containing the text * (if none found, return -1) */ */ public int searchNotes (String searchString) {... code body of the method... code body of the method} -1 is not a valid index for a collection. So if that it what is returned, it means no note containing the search string was found. Otherwise, the number returned will be a valid index (between 0 and one less than the size of the notebook).

17 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public int searchNotes (String searchString) { int index = 0; int index = 0; boolean found = false; // so far, not found it boolean found = false; // so far, not found it while ((!found) && (index < notes.size())) { while ((!found) && (index < notes.size())) { String note = notes.get(index); String note = notes.get(index); if (note.contains(searchString)) { if (note.contains(searchString)) { // stop here - no need to keep looking. // stop here - no need to keep looking. found = true; found = true; } else { else { index++; // index = index + 1 index++; // index = index + 1 } } if (found) {return index;} else {return -1;} } if (found) {return index;} else {return -1;}} Either we found a note containing the search string (in which case index tells us which one)... Or we searched the whole collection, didn’t find it and found is still false (and index is notes.size(), which is not a valid index).

18 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Either we found a note containing the search string (in which case we returned the index telling us which one)... Or we searched the whole collection, didn’t find it and returned -1. public int searchNotes (String searchString) { int index = 0; int index = 0; while (index < notes.size()) { while (index < notes.size()) { String note = notes.get(index); String note = notes.get(index); if (note.contains(searchString)) { if (note.contains(searchString)) { // stop here - no need to keep looking. // stop here - no need to keep looking. return index; return index; } else { else { index++; // index = index + 1 index++; // index = index + 1 } } return -1; } return -1;} alternative coding 1 exits the method, jumping straight out of the loop!

19 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Either we found a note containing the search string (in which case we returned the index telling us which one)... Or we searched the whole collection, didn’t find it and returned -1. public int searchNotes (String searchString) { int index = 0; int index = 0; for (String note : notes) { for (String note : notes) { if (note.contains(searchString)) { if (note.contains(searchString)) { // stop here - no need to keep looking. // stop here - no need to keep looking. return index; return index; } else { else { index++; // index = index + 1 index++; // index = index + 1 } } return -1; } return -1;} alternative coding 2 exits the method, jumping straight out of the loop!

20 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling loop statements enable a block of statements to be repeated. loop statements enable a block of statements to be repeated. The for-each loop enables iteration over a whole collection. The for-each loop enables iteration over a whole collection. The while loop enables iteration over part of a collection … or for iteration that doesn’t involve a collection. The while loop enables iteration over part of a collection … or for iteration that doesn’t involve a collection. Review

21 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Review The for-each loop enables iteration over a whole collection. The for-each loop enables iteration over a whole collection. Actually, by using a return, a for-each loop can have an early exit – thus, enabling iteration over (the first) part of a collection. ** For our searchNotes example, this happens to yield the simplest coding …


Download ppt "Grouping Objects 2 Collections and the for-each loop Collections and the while loop."

Similar presentations


Ads by Google