Title: Grouping objects
1Grouping objects
- Arrays, Collections and Iterators
1.0
2Main concepts to be covered
- Arrays
- Collections
- Iterators
3Requirement 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
4Fixed-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?
5Creating an array object
public class LogAnalyzer private int
hourCounts private LogfileReader reader Â
public LogAnalyzer() hourCounts
new int24 reader new
LogfileReader() ...
Array variable declaration
Array object creation
6The hourCounts array
7Using an array
- Square-bracket notation is used to access an
array element hourCountshour - Elements are used like ordinary variables
- In an expression
- adjusted hourCountshour 3
- hourCountshour
8Class 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
9A personal notebook
- Notes may be stored
- No limit to the number of notes
- It tells how many notes are stored
10import 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() ...
11Object structures with collections
12Features of ArrayList
- It keeps the objects in order
- It increases its capacity as necessary
- It keeps a private count size() accessor
retrieves it
13Using 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).
14Adding a third note
myBook.add(1130 meet John)
15Retrieving an object
public void showNote(int noteNumber)
if(noteNumberlt0 noteNumber gt numberOfNotes())
// This is not a valid note number.
else System.out.println(notes.get(noteNumber))
Retrieve and print the note
16Removal may affect numbering
myBook.remove(1)
17Review 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
18Collections framework
- Goals comparable to Cs STL
- Data structures
- Vector, LinkedList, HashSet, etc.
- Algorithms
- Sorting, searching, shuffling, etc.
19Loops and bugs
- Why are loops often a cause of bugs?
- What kind of bugs?
- Wouldnt be nice to avoid these bugs?
20Iterating over a collection
Returns an Iterator object
java.util.Iterator
Iterator it myCollection.iterator() while(it.ha
sNext()) call it.next() to get the next
object do something with that object
public void listNotes() Iterator it
notes.iterator() while(it.hasNext())
System.out.println(it.next())
How does Iterator help avoid bugs?
21Review 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