Grouping objects Arrays, Collections and Iterators 1.0.

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

Grouping objects Arrays and for loops. Fixed-size collections Sometimes the maximum collection size can be pre-determined. Programming languages usually.
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.
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.
Modul 3 Collections af objekter Arraylist Collections Objektorienteret design og Java. 4.0.
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.
ArrayLists.  The Java API includes a class named ArrayList. The introduction to the ArrayList class in the Java API documentation is shown below.  java.lang.Object.
DREW ALVAREZ AND CORDIE GOODRICH ARRAYS AND ARRAY LISTS.
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
111 © 2002, Cisco Systems, Inc. All rights reserved.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.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)
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.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
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.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
What is Iterator Category: Behavioral Generic Way to Traverse Collection Not Related to Collection Implementation Details Not Related to the direction/fashion.
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.
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.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
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 9:FXML and Useful Java Collections Michael Hsu CSULA.
Fixed-sized collections Introduction to arrays 6.0.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
Coming up ArrayList ArrayList vs Array – Declaration – Insertion – Access – Removal Wrapper classes Iterator object.
Chapter 8: Understanding Collections Textbook: Chapter 4.
Objects First with Java CITS1001 week 4
Objects First with Java Introduction to collections
Fixed-sized collections
JAVA COLLECTIONS LIBRARY
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 Framework
COS 260 DAY 11 Tony Gauvin.
Collections and iterators
Collections and iterators
Presentation transcript:

Grouping objects Arrays, Collections and Iterators 1.0

2 Main concepts to be covered Arrays Collections Iterators

3 Requirement to group objects Many applications for collections of objects: –Personal organizers –Library catalogs –Student-record system The number of items to be stored varies: –Items added –Items deleted

4 Fixed-size collections Programming languages usually offer a special fixed-size collection type: an array Arrays are built-in, use [] syntax Java arrays can store objects or primitive-type values Maximum collection size must be fixed at Array creation time How is Array creation time more dynamic than in other programming languages, such as C++?

5 Creating an array object public class LogAnalyzer { private int[] hourCounts; private LogfileReader reader; public LogAnalyzer() { hourCounts = new int[24]; reader = new LogfileReader(); }... } Array object creation Array variable declaration

6 The hourCounts array

7 Using an array Square-bracket notation is used to access an array element: hourCounts[hour] Elements are used like ordinary variables –In an expression: adjusted = hourCounts[hour] – 3; hourCounts[hour]++;

8 Class libraries Collections of useful classes Encourages reuse of design and code Java organizes its libraries in packages The java.util package includes classes for grouping objects in collections

9 A personal notebook Notes may be stored No limit to the number of notes It tells how many notes are stored

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

11 Object structures with collections

12 Features of ArrayList It keeps the objects in order It increases its capacity as necessary It keeps a private count: size() accessor retrieves it

13 Using ArrayList 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).

14 Adding a third note myBook.add(“11:30 meet John”)

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

16 Removal may affect numbering myBook.remove(1)

17 Review: ArrayList 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 For more methods, see API documentAPI document

18 Collections framework Goals comparable to C++’s STL Data structures: –Vector, LinkedList, HashSet, etc. Algorithms: –Sorting, searching, shuffling, etc.

19 Loops and bugs Why are loops often a cause of bugs? What kind of bugs? Wouldn’t be nice to avoid these bugs?

20 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()); } How does Iterator help avoid bugs?

21 Review: loops and Iterator Java while and for loops are similar to C++ –Similar bugs, too! Collection classes have special Iterator objects that simplify iteration over the whole collection –Bounds-checking methods help avoid bugs