21
Grouping objects Arrays, Collections and Iterators 1.0

Grouping objects Arrays, Collections and Iterators 1.0

Embed Size (px)

Citation preview

Page 1: Grouping objects Arrays, Collections and Iterators 1.0

Grouping objects

Arrays, Collections and Iterators

1.0

Page 2: Grouping objects Arrays, Collections and Iterators 1.0

2

Main concepts to be covered

• Arrays • Collections• Iterators

Page 3: Grouping objects Arrays, Collections and Iterators 1.0

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

Page 4: Grouping objects Arrays, Collections and Iterators 1.0

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++?

Page 5: Grouping objects Arrays, Collections and Iterators 1.0

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

Page 6: Grouping objects Arrays, Collections and Iterators 1.0

6

The hourCounts array

Page 7: Grouping objects Arrays, Collections and Iterators 1.0

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]++;

Page 8: Grouping objects Arrays, Collections and Iterators 1.0

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

Page 9: Grouping objects Arrays, Collections and Iterators 1.0

9

A personal notebook

• Notes may be stored• No limit to the number of notes• It tells how many notes are

stored

Page 10: Grouping objects Arrays, Collections and Iterators 1.0

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

...}

Page 11: Grouping objects Arrays, Collections and Iterators 1.0

11

Object structures with collections

Page 12: Grouping objects Arrays, Collections and Iterators 1.0

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

Page 13: Grouping objects Arrays, Collections and Iterators 1.0

13

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

Page 14: Grouping objects Arrays, Collections and Iterators 1.0

14

Adding a third notemyBook.add(“11:30 meet John”)

Page 15: Grouping objects Arrays, Collections and Iterators 1.0

15

Retrieving an object

Retrieve and print the note

public void showNote(int noteNumber){ if(noteNumber<0 && noteNumber >= numberOfNotes()) { // This is not a valid note number. } else System.out.println(notes.get(noteNumber));}

Page 16: Grouping objects Arrays, Collections and Iterators 1.0

16

Removal may affect numbering

myBook.remove(1)

Page 17: Grouping objects Arrays, Collections and Iterators 1.0

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 document

Page 18: Grouping objects Arrays, Collections and Iterators 1.0

18

Collections framework

• Goals comparable to C++’s STL

• Data structures:– Vector, LinkedList, HashSet, etc.

• Algorithms:– Sorting, searching, shuffling, etc.

Page 19: Grouping objects Arrays, Collections and Iterators 1.0

19

Loops and bugs

• Why are loops often a cause of bugs?

• What kind of bugs?• Wouldn’t be nice to avoid these

bugs?

Page 20: Grouping objects Arrays, Collections and Iterators 1.0

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.IteratorReturns an Iterator

object

public void listNotes(){ Iterator it = notes.iterator(); while(it.hasNext()) { System.out.println(it.next()); }}

How does Iterator help avoid bugs?

Page 21: Grouping objects Arrays, Collections and Iterators 1.0

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