Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.

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

Grouping objects Arrays and for loops. Fixed-size collections Sometimes the maximum collection size can be pre-determined. Programming languages usually.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
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.
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.
Programming with Collections Collections in Java Using Arrays Week 9.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
Grouping Objects 2 Collections and the for-each loop Collections and the while loop.
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.
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.
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Goals for Today  implement a Deck of Cards  composition  Iterator interface  Iterable interface 1.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
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.
Grouping objects Arrays, Collections and Iterators 1.0.
Grouping objects Introduction to collections 5.0.
1 CSC 221: Computer Programming I Fall 2004 Lists, data access, and searching  ArrayList class  ArrayList methods: add, get, size, remove  example:
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
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)
Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java
OOP (Java): Grouping/ OOP (Java) Objectives – –discuss call-by-value and call-by-reference, arrays, collections, and iterators Semester.
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
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.
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.
Programming Fundamentals 2: Grouping/ F II Objectives – –discuss call-by-value and call-by-reference, arrays, collections, and iterators.
Grouping objects Introduction to collections 5.0.
1 COS 260 DAY 7 Tony Gauvin. 2 Agenda Questions? 3 rd Mini quiz next class (September 28) –Chap 3 concepts Assignment 1 Corrected Assignment 2 posted.
Comparing ArrayLists and Arrays. ArrayLists ArrayLists are one type of flexible-size collection classes supported by Java –ArrayLists increase and decrease.
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
Collections Dwight Deugo Nesa Matic
Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)
Fixed-sized collections Introduction to arrays 6.0.
Grouping objects Introduction to collections 6.0.
Comp1004: Environments The Java Library. Coming up Recap – Encapsulation – Constructors – Loops – Arrays – ArrayList – Iterators The Java Library – Implementation.
Functional Processing of Collections (Advanced) 6.0.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Coming up ArrayList ArrayList vs Array – Declaration – Insertion – Access – Removal Wrapper classes Iterator object.
Chapter 8: Understanding Collections Textbook: Chapter 4.
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
Lecture 4b Repeating With Loops
Objects First with Java Introduction to collections
COS 260 DAY 7 Tony Gauvin.
Functional Processing of Collections (Advanced)
COS 260 DAY 9 Tony Gauvin.
More sophisticated behavior
Objects First with Java Introduction to collections
Lecture Notes – Week 3 Lecture-2
ArrayList Collections.
COS 260 DAY 8 Tony Gauvin.
COS 260 DAY 8 Tony Gauvin.
Object Oriented Programming in java
Objects First with Java Introduction to collections
F II 5. Grouping Objects Objectives
Collections and iterators
Collections and iterators
Presentation transcript:

Grouping objects Collections and iterators

Main concepts to be covered Collections Loops Iterators

The requirement to group objects Many applications involve collections of objects: Personal organizers. Library catalogs. Online shopping. The number of items to be stored varies. Items added. Items deleted.

A personal notebook Notes may be stored. Individual notes can be viewed. There is no limit to the number of notes. It will tell how many notes are stored.

Lecture 4. Grouping objects 5 Pablo Romero, Department of Informatics import java.util.ArrayList; /** *... */ public class Notebook { // Storage for an arbitrary number of notes. private ArrayList notes; /** * Perform any initialization required for the * notebook. */ public Notebook() { notes = new ArrayList (); }... } Field declaration Field initialisation

Object structures with collections

Adding a third note

Using the collection public class Notebook { private ArrayList notes;... public void storeNote(String note) { notes.add(note); } public int numberOfNotes() { return notes.size(); }... } Adding a new note Returning the number of notes (delegation).

Index numbering

Index validity checks Retrieving an object Retrieve and print the note public void showNote(int noteNumber) { if(noteNumber < 0) { // This is not a valid note number. } else { if(noteNumber < numberOfNotes()) { System.out.println(notes.get(noteNumber)); } else { // This is not a valid note number. }

Removal may affect numbering (deleting the second note)

Review Collections allow an arbitrary number of objects to be stored. Class libraries (packages) usually contain tried-and-tested collection classes. We have used the ArrayList class from the java.util package. To use hash maps we would need to use the HashMap class

Review Items may be added and removed. Each item has an index. Index values may change if items are removed (or further items added). The main ArrayList methods are add, get, remove and size.

Iteration We often want to perform some actions an arbitrary number of times. E.g., print all the notes in the notebook. How many are there? Most programming languages include loop statements to make this possible. Three sorts of loop statement. for-each loop while loop for loop

For-each loop pseudo code for(ElementType element: collection) { loop body } Loop variable for keyword Statements to be repeated General form of a for-each loop for each(note in the notes collection) { show the next note } Pseudo-code example to print every note Our collection

A Java example /** * List all notes in the notebook. */ public void listNotes() { for(String note : notes) { System.out.println(note); } Loop variable How many times is this executed?

While loop pseudo code while(loop condition) { loop body } Boolean test while keyword Statements to be repeated General form of a while loop while(there is at least one more note to be printed) { show the next note } Pseudo-code example to print every note

A Java example /** * List all notes in the notebook. */ public void listNotes() { int index = 0; while(index < notes.size()) { System.out.println(notes.get(index)); index++; } Increment by one Local index variable

Exercise Write the method noteExists(String searchString) that searches the notebook for a specific string and returns true if any of the strings in the notebook contains the parameter and false otherwise. You might need to use a variable of type boolean (which can take the literal values true or false) and the contains method from the String class which takes a parameter of type string and returns true if the parameter is included in the string and false otherwise.

For loop pseudo-code for(initialization; condition; post-body action) { statements to be repeated } General form of a for loop Equivalent in while-loop form initialization; while(condition) { statements to be repeated post-body action }

Lecture 4. Grouping objects 21 Pablo Romero, Department of Informatics for(int index = 0; index < notes.size(); index++) { System.out.println(notes.get(index)); } for loop version while loop version public void listNotes() { int index = 0; while(index < notes.size()) { System.out.println(notes.get(index)); index++; }

Three types of loops for-each if you need to iterate over all elements of a collection while or for if the iteration is not related to a collection for when the number of iterations is known while when number of iterations is determined on the fly

Iterators while loop made simple Special classes to iterate over collections Have methods to Check whether there are more elements Obtain the next element

Iterating over a collection Iterator it = myCollection.iterator(); while(it.hasNext()) { call it.next() to get the next object do something with that object } java.util.Iterator Returns an Iterator object public void listNotes() { } Iterator it = notes.iterator(); while(it.hasNext()) { System.out.println(it.next()); }

Review Loop statements allow a block of statements to be repeated. Three types of loops for-each while for Collection classes have special Iterator objects that simplify iteration over the whole collection.