Grouping Objects 3 Iterators, collections and the while loop.

Slides:



Advertisements
Similar presentations
Grouping objects Iterators. Iterator and iterator() Collections have an iterator() method. This returns an Iterator object. Iterator has three methods:
Advertisements

Objects First With Java A Practical Introduction Using BlueJ Improving structure with inheritance 2.0.
Improving structure with inheritance Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts.
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.
Improving structure with inheritance
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.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
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 2 Collections and the for-each loop Collections and the while loop.
Grouping Objects 1 Introduction to Collections.
Make Sure You Know All This!. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 2 Objects and Classes.
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.
STREAM STREAM A software process The Mañana Principle The Mañana Principle Don’t try to everything at once! Static Methods Static Methods Stateless Utility.
© 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.
CS2110 Recitation 07. Interfaces Iterator and Iterable. Nested, Inner, and static classes We work often with a class C (say) that implements a bag: unordered.
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
REPETITION CITS1001. Scope of this lecture Repetition for loops while loops 2.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
Grouping objects Arrays, Collections and Iterators 1.0.
1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS.
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.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
JAVA COLLECTIONS M. TAIMOOR KHAN (ADAPTED FROM SWINBURNE NOTES)
Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds.
Understanding class definitions
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
Comp1004: Inheritance I Super and Sub-classes. Coming up Inheritance and Code Duplication – Super and sub-classes – Inheritance hierarchies Inheritance.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 1.0.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
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.
Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)
Java I--Copyright © Tom Hunter. Chapter 4 Control Structures: Part I.
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
Review. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Objects and Classes Objects and Classes –State.
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
Objects First with Java Introduction to collections
Functional Processing of Collections (Advanced)
COS 260 DAY 9 Tony Gauvin.
Objects First with Java Introduction to collections
"First things first, but not necessarily in that order " -Dr. Who
Iterator.
COS 260 DAY 8 Tony Gauvin.
Objects First with Java Introduction to collections
COS 260 DAY 11 Tony Gauvin.
Collections and iterators
Improving structure with inheritance
Collections and iterators
Presentation transcript:

Grouping Objects 3 Iterators, collections and the while loop

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.

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

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

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

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

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 ✔

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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

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

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.

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

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

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

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;

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.