Programming with Collections Grouping & Looping - Collections and Iteration Week 7.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Lab 10: Bank Account II Transaction List, error checking & aggregation.
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.
More sophisticated behavior Using library classes to implement some more advanced functionality 4.0.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
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.
Week 11 Recap CSE 115 Spring Want to write a programming language? You’ll need three things: You’ll need three things: –Sequencing –Selection Polymorphism.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Modul 3 Collections af objekter Arraylist Collections Objektorienteret design og Java. 4.0.
Understanding class definitions Looking inside classes.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
High-Level Programming Languages: C++
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.
Chapter 11 Arrays Continued
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
Grouping objects Arrays, Collections and Iterators 1.0.
The Java Collections Framework By the end of this lecture you should be able to: use the ArrayList class to store a list of objects; use the HashSet class.
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.
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)
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.
Written by: Dr. JJ Shepherd
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.
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(…)
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,
Functional Processing of Collections (Advanced) 6.0.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
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
Objects First with Java Introduction to collections
COS 260 DAY 7 Tony Gauvin.
Functional Processing of Collections (Advanced)
Objects First with Java Introduction to collections
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Lecture Notes – Week 3 Lecture-2
Collections & Definite Loops
COS 260 DAY 8 Tony Gauvin.
COS 260 DAY 8 Tony Gauvin.
Object Oriented Programming in java
Objects First with Java Introduction to collections
Collections and iterators
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Collections and iterators
Presentation transcript:

Programming with Collections Grouping & Looping - Collections and Iteration Week 7

 Collections  ArrayList objects  Collections and loops Programming with Collections CONCEPTS COVERED THIS WEEK

The requirement to group objects Many applications involve collections of objects: –Personal organizers. –Library catalogs. –Student-record system. 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. Explore the Notebook project.

Class libraries Collections of useful classes. We don’t have to write everything from scratch. Java calls its libraries, packages. Grouping objects is a recurring requirement. –The java.util package contains classes for doing this.

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

Collections We specify: – the type of collection: ArrayList –the type of objects it will contain: We say, “ArrayList of String objects”.

Object structures with collections

Adding a third note

Features of the collection It increases its capacity as necessary. It keeps a private count ( size() accessor). It keeps the objects in order. Details of how all this is done are hidden. –Does that matter? Does not knowing how prevent us from using it?

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

Retrieving an object Index validity checks 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. }

Removing an object notes.remove(noteNumber); When an object is removed, objects with higher index values have their index values reduced by one, i.e. each of them is moved along by one position to fill the gap

Removal may affect numbering

Generic classes Collections are known as parameterized or generic types. ArrayList implements list functionality: –add, get, remove, size, etc. The type parameter says what we want a list of: –ArrayList –etc.

Review Collections allow an arbitrary number of objects to be stored. Class libraries usually contain tried-and- tested collection classes. Java’s class libraries are called packages. We have used the ArrayList class from the java.util package.

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. ArrayList is a parameterized or generic type.

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. Java has several sorts of loop statement. –We will start with its for-each loop.

Iteration fundamentals We often want to repeat some actions over and over. Loops provide us with a way to control how many times we repeat those actions. With collections, we often want to repeat things once for every object in a particular collection.

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

A Java example /** * List all notes in the notebook. */ public void listNotes() { for(String note : notes) { System.out.println(note); } for each note in notes, print out note

The while loop A for-each loop repeats the loop body for each object in a collection. Sometimes we require more variation than this. We can use a boolean condition to decide whether or not to keep going. A while loop provides this control.

While loop pseudo code while(loop condition) { loop body } while we wish to continue, 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

/** * List all notes in the notebook. */ public void listNotes() { int index = 0; while(index < notes.size()) { System.out.println(notes.get(index)); index++; } A Java example Increment index by 1 while the value of index is less than the size of the collection, print the next note, and then increment index

for-each versus while for-each: –easier to write. –safer: it is guaranteed to stop. while: –we don’t have to process the whole collection. –doesn’t even have to be used with a collection. –take care: could be an infinite loop.

While without a collection // Print all even numbers from 0 to 30. int index = 0; while(index <= 30) { System.out.println(index); index = index + 2; }

Searching a collection int index = 0; boolean found = false; while(index < notes.size() && !found) { String note = notes.get(index); if(note.contains(searchString)) { // We don't need to keep looking. found = true; } else { index++; } // Either we found it, or we searched the whole // collection.

Methods of String objects string1.length() returns the number of characters in string1 as an int string1.equals(string2) returns a boolean, which takes the value true if the strings consist of the same sequence of characters string1.contains(string2) returns a boolean, which takes the value true if string1 contains the sequence of characters given by string2

Review Loop statements allow a block of statements to be repeated. The for-each loop allows iteration over a whole collection. The while loop allows the repetition to be controlled by a boolean expression.

Required Reading Objects First With Java – A Practical Introduction using BlueJ Related reading for this lecture Chapter 4 – pp