Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 1.0.

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:
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.
More sophisticated behaviour Using library classes to implement some more advanced functionality.
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.
Understanding class definitions Looking inside classes 3.0.
More sophisticated behavior Using library classes to implement some more advanced functionality 4.0.
Generic Classes and 'for each' Loops List zoo = new ArrayList (); // … add several Animals to the zoo for (Animal animal : zoo) // do something with animal.
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.
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.
More about inheritance Exploring polymorphism 3.0.
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.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
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.
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)
Understanding class definitions
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.
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.
Objects First With Java A Practical Introduction Using BlueJ Casting Week
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.
Week111 APCS-A: Java Arrays Continued (related information in Chapter 7 of Lewis & Loftus) November 16, 2005.
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.
Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Grouping objects Introduction to collections 6.0.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
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.
Chapter 8: Understanding Collections Textbook: Chapter 4.
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)
COS 260 DAY 9 Tony Gauvin.
More sophisticated behavior
Objects First with Java Introduction to 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
Understanding class definitions
Collections and iterators
Improving structure with inheritance
Collections and iterators
Presentation transcript:

Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 1.0

2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to be covered Collections Loops (gdb: I dropped this part) Iterators Arrays (gdb: I dropped this, too)

3 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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.

4 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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 notebook1 project.

5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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.

6 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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(); }... }

7 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Object structures with collections

8 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Adding a third note

9 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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?

10 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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).

11 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Index numbering

12 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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. }

13 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Removal may affect numbering

14 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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.

15 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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.

16 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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()); }

17 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. Two further points to follow up: –The null value. –Casting. Used to store the result of get into a variable: String message = (String) notes.get(0);

18 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. A Java while loop allows the repetition to be controlled by a boolean expression. Collection classes have special Iterator objects that simplify iteration over the whole collection.