Overview
- Collection Classes
- Java Arrays
- Cloning Arrays
- Abstract Data Type for a Bag of Integers:
- Bags, Sets, Sequences
- Specification: selected methods
- Implementation: clone
- quiz, Chapters 1 and 2
Collection Classes
- in a collection class, each object contains a collection of
elements
- examples: test scores for a course can be stored in a collection class
- examples: colors for a sampler can be stored in a collection class
- some languages distinguish two kinds of collection types:
- uniform: all elements have the same type
- non-uniform: elements may have the same type
This course will have examples of both uniform and non-uniform collection
classes.
Java Arrays Review
- arrays are themselves collections
- "int [] myArray;" is a uniform array of integers
- "myArray = new int [10];" creates an array with 10 elements
numbered 0... 9.
- myArray.length == 10
- "myArray [7] = 19;" stores the value 19 into the 8th element
of the array.
- "x = myArray [3];" gets the fourth element from the array
and stores the value in x.
- NullPointerException is thrown for using an
array that is null
- ArrayIndexOutOfBoundsException is thrown for using an
index that is out of bounds
Cloning Arrays and Typecasts
- as for all classes, assignment makes a copy of the reference, not
of the array
- clone method will copy the array
- copy = (int []) myArray.clone
- the type declaration in this assignment is called a type cast: it
converts a value of type object to a value of type int array
- the typecast is needed because clone always returns an object
- the typecast will fail if the value is of the wrong underlying type
- example: casting a clone of MyClass to (int []) will
throw ClassCastException
Array properties
- cannot grow or shrink -- fixed number of elements
- efficient random access to elements, by index -- O(1), i.e. constant time
- efficient sequential access -- O(1) for one access
- duplicate elements allowed
- slow search -- O(n)
Bags, Sets, Sequences
- bags:
- can grow or shrink
- duplicate elements allowed
- no indices, no random access
- usually fast addition, slow search, deletion
- sets are like bags, but:
- no duplicate elements
- addition, search deletion can be O(1)
- sequences:
- can grow or shrink
- efficient access (addition, deletion) to the front and sometimes the back
- duplicate elements allowed
A Bag of Integers
- used to store a collection of integers, with possible duplicates
- constructor, add, remove, size, countHowMany
- addAll (join, merge two bags) and union (same as addAll, but
result is a new bag)
- pre-allocate and control the capacity: get, increase,
reduce the guaranteed capacity of the bag
- OutOfMemoryError exception may be thrown when changing the
capacity
Specification
- public class IntArrayBag
- a collection of int numbers
- limitations:
- maximum capacity limited by available memory --
may throw OutOfMemoryError
- maximum capacity limited by
Integer.MAX_VALUE = 2^{31} - 1 = 2,147,843,647
- some operations require time O(n) for a bag of size n
- constructor public IntArrayBag() -- initial
capacity is 10
- constructor public IntArrayBag(int initialCapacity)
Specification for add
- public void add (int element)
- adds the new element to the bag, increasing the capacity if necessary
- parameter: element is the element to add to the bag
- postcondition: a copy of the element has been added to the bag
- throws: OutOfMemoryError
- note: incrementing capacity beyond Integer.MAX_VALUE causes
arithmetic overflow
Specification for remove
- public boolean remove (int target)
- if possible, removes one copy of the element from the bag
- parameter: target is the element to remove from the bag
- postcondition: if at least one instance of target was
present in the bag, one copy is removed and the method returns
true. Otherwise, the bag is unchanged and the method returns
false.
Specification for clone
- public Object clone ()
- generates a copy of this bag
- returns: a new object whose contents are identical to those of
this bag
- throws: OutOfMemoryError
Implementation of clone
public class IntArrayBag
implements Cloneable
{
private int [] data;
...
public Object clone()
{
IntArrayBag answer;
try {
answer = (IntArrayBag) super.clone();
} catch (CloneNotSupportedException e)
{
...
}
answer.data = (int []) data.clone ();
return answer;
}