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

Slides:



Advertisements
Similar presentations
More Sophisticated Behaviour 1 Using library classes to implement more advanced functionality.
Advertisements

Grouping objects Iterators. Iterator and iterator() Collections have an iterator() method. This returns an Iterator object. Iterator has three methods:
Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
1 Features of the collection It increases its capacity as necessary. It keeps a private count: –size() accessor. It keeps the objects in order. Details.
Grouping objects Introduction to collections 5.0.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
Using Collections. Review of Collections Using an ArrayList It increases its capacity as necessary. It keeps a private count ( size() accessor). It keeps.
Grouping Objects 3 Iterators, collections and the while loop.
String Concatenation (operator overloading) 3.0.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
More sophisticated behavior Using library classes to implement some more advanced functionality 4.0.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Grouping Objects 1 Introduction to Collections.
Grouping objects Collections and iterators. 04/11/2004Lecture 4: Grouping Objects2 Main concepts to be covered Collections Loops Iterators Arrays.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
1 Reflecting on the ticket machines Their behavior is inadequate in several ways: –No checks on the amounts entered. –No refunds. –No checks for a sensible.
More sophisticated behavior Using library classes to implement some more advanced functionality 3.0.
Modul 3 Collections af objekter Arraylist Collections Objektorienteret design og Java. 4.0.
Programming with Collections Grouping & Looping - Collections and Iteration Week 7.
Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number.
The switch Statement, DecimalFormat, and Introduction to Looping
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
REPETITION CITS1001. Scope of this lecture Repetition for loops while loops 2.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Grouping objects Introduction to collections 5.0.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Introduction to Java Java Translation Program Structure
JAVA COLLECTIONS M. TAIMOOR KHAN (ADAPTED FROM SWINBURNE NOTES)
Understanding class definitions
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
Testing. Have you contacted your project members lately?
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 1.0.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Grouping objects Arrays. 2 Fixed-size collections The maximum collection size may be pre-determined with an upper limit Array is an fixed-size collection.
Grouping objects Introduction to collections 5.0.
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Grouping objects Introduction to collections 6.0.
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Coming up ArrayList ArrayList vs Array – Declaration – Insertion – Access – Removal Wrapper classes Iterator object.
1 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. for-each PROS easy to use access to ALL items one-by-one ability to change the state.
for-each PROS CONS easy to use access to ALL items one-by-one
Objects First with Java CITS1001 week 4
Chapter 4 Repetition Statements (loops)
Objects First with Java Introduction to collections
The switch Statement, and Introduction to Looping
Functional Processing of Collections (Advanced)
COS 260 DAY 9 Tony Gauvin.
Objects First with Java Introduction to collections
Understanding class definitions
COS 260 DAY 10 Tony Gauvin.
COS 260 DAY 8 Tony Gauvin.
COS 260 DAY 8 Tony Gauvin.
Understanding class definitions
Collections and iterators
COS 260 DAY 4 Tony Gauvin.
Improving structure with inheritance
Collections and iterators
Presentation transcript:

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

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

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

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)

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

/** * 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

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

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.

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

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

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

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)

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!

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. * searchString The sought text 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. * searchString The sought text 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).

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

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!

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!

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

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 …