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.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Generics, Lists, Interfaces
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Programming Logic and Design, Third Edition Comprehensive
Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create 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.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
List Implementations That Use Arrays Chapter 5. 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation.
Generic Classes and 'for each' Loops List zoo = new ArrayList (); // … add several Animals to the zoo for (Animal animal : zoo) // do something with animal.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
Grouping Objects 1 Introduction to Collections.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
Grouping objects Collections and iterators. 04/11/2004Lecture 4: Grouping Objects2 Main concepts to be covered Collections Loops Iterators Arrays.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Programming with Collections Grouping & Looping - Collections and Iteration Week 7.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
Arrays And ArrayLists - S. Kelly-Bootle
M1G Introduction to Programming 2 4. Enhancing a class:Room.
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
4.1 Instance Variables, Constructors, and Methods.
ArrayList, Multidimensional Arrays
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
Grouping objects Arrays, Collections and Iterators 1.0.
Data structures Abstract data types Java classes for Data structures and ADTs.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
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.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
JAVA COLLECTIONS M. TAIMOOR KHAN (ADAPTED FROM SWINBURNE NOTES)
Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
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.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Grouping objects Introduction to collections 5.0.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
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.
Lecture 8: Collections, Comparisons and Conversions. Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
Coming up ArrayList ArrayList vs Array – Declaration – Insertion – Access – Removal Wrapper classes Iterator object.
Chapter 8: Understanding Collections Textbook: Chapter 4.
EKT472: Object Oriented Programming
Objects First with Java CITS1001 week 4
Objects First with Java Introduction to collections
Introduction to Linked Lists
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Arrays and Collections
Collections and iterators
Collections and iterators
Presentation transcript:

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 that the number of items to be stored will vary over time  We could create a class with a lot of fixed fields, but in general this will not work

Personal Notebook Example  We will model a personal notebook  Allow notes to be stored  No limit to the number of notes stored  Show individual notes  Tells how many notes are stored

Library Classes  One feature of OO languages, is the accompanying class libraries  Library typically contain hundreds or thousands of different classes  Java calls these libraries packages  We use library classes exactly the same way we would our own classes  Instances are constructed using new, and the classes have fields, constructors and methods

Actual Code  Let’s look at the Notebook class code.  Notebook Project Notebook Project Notebook Project  The very first line shows how we access the package, using an import statement  import java.util.ArrayList;  Import statements must always be placed before class definitions in a file.  Once a class name has been imported, we can use that class as if it were our own.

Object Structures with Collections  There are at least three important features  It is able to increase its internal capacity as required: as more items are added, it simply makes enough room for them.  It keeps its own private count of how many items it is currently storing. Its size method returns the number of objects currently stored  It maintains the order of items you insert into it. You can later retrieve them in the same order.

Numbering within Collections  It is necessary to use values starting at zero.  Items are stored in the ArrayList starting a number position zero  The position is known as the index  First is given index 0, the next is index 1, …  It is a common mistake to try to access a collection outside the valid indices.

Removing Elements  When a user wants to remove an note, we can invoke the remove method of the notebook object  One complication of the removal process is that it can change the index values at which notes are stored  If a note with a low index value is removed, then the collection moves all notes forward to fill the hole.  Furthermore, it is possible to insert into other than at the end.

Processing a whole collection  It would be useful to list all the notes in the notebook it find where all of them are  We have the need to do something several times, but the number of times depends upon circumstances that vary.  The solution is the while loop; one of Java’s loop statements

The while loop  A while loop is one way to perform a set of actions repeatedly, without having to write those actions more than once.  Here is a general format while ( loop condition ) { loop body //statements you want to //repeat}

Example /* *List all notes in the notebook *List all notes in the notebook */ */ public void listNotes() { int index = 0; while ( index < notes.size() ) {System.out.println(notes.get(index));index++;}}

While vs. If  Do not confuse a while loop with an if.  Although they look similar, they operate in very different ways  The biggest difference is that once the body of the loop has been executed for the first time, we go back to the test again to see if the body should be executed.

Iterators  Examining every item in a collection is a very common activity.  The ArrayList class supplies an iterator method that supplies an Iterator object that allows us to iterate over the collection  We must add another import statement to use the iterator object  import java.util.Iterator;

Iterators  An Iterator supplies two methods to iterate over a collection: hasNext and next Iterator it = myCollection.iterator(); while ( it.hasNext() ) { //call it.next() to get the //next object and do something //with that object }

Indices vs. Iterators  We have seen approaches to accessing elements in an ArrayList  In this example, both seem equally adequate  Java supplies many other collections that are not as straight foreword  It may also be inefficient or impossible to access elements through indices.  All Java collections have iterators

The Auction System Example  The Auction System Example The Auction System Example The Auction System Example  highestBid == null  null is a Java key word and means ‘no object’  When a field is an object type and it is not explicity initialized in a constructor, that field will contain the value null

Casting  Casting is an important feature of Java Lot lot = (Lot) it.next();  A cast consists of the name of a type written alone between a pair of parentheses  Casting is commonly seen when retrieving objects from a collection  This is required because collections can store any type of object  Casting makes it clear what type of object it is