INFS 519 - September 6, 2011
Table of Contents
Reading
This lecture covers material from chapters 3 and 4.
Object
methods
clone
- Create and return a copy of this object
equals
- Returns true if this object is equal to the other
toString
- Return the string representation of this object
Collection classes
Java arrays
- 0-based index
- Arrays of any type
- Random access
Reference variables
float[] lastWeek; float[] thisWeek; lastWeek = new float[5]; lastWeek[0] = 8; lastWeek[1] = 7.5; lastWeek[2] = 8.5; lastWeek[3] = 10.0; lastWeek[4] = 6; thisWeek = lastWeek;
Clone
thisWeek = lastWeek.clone();
Iterating over arrays
- With index variable
public static void reset(float[] vals) { int i; for (i = 0; i < vals.length; i++) { vals[i] = 0.0; } }
- With iterator
// Don't do this!!! public static void reset(float[] vals) { for (float item : vals) { // We're setting item to 0.0, but the array isn't being updated. item = 0.0; } } // But this works great public static float sum(float[] vals) { // Initialize your variables float sum = 0.0; // Loop over all the values, storing the sum for (float item : vals) { sum += item; } return sum; }
Bag
- A collection type for storing items
- Unordered
- Items with the same value can be stored multiple times
- Also known as a multiset
IntArrayBag
An implementation of a bag that stores values of type int
and uses
an Array
.
- Specification
- Constructors
public IntArrayBag(int initialCapacity)
- Instance methods
public void add(int element)
public void addMany(int... elements)
public boolean remove(int target)
public int size()
public int countOccurrences(int target)
public void trimToSize()
- Static methods
public static IntArrayBag union(IntArrayBag b1, IntArrayBag b2)
- Constructors
Review implementation of IntArrayBag
Storage management
An array is used to store the values. The ensureCapacity
method is called when
new items will be added to ensure that the backing array is capable of storing all values.
If it is too small, it will be resized and made larger.
Invariant
- Rules that should always hold
- "[I]f the predicate is true before starting the sequence [of operations], then it is true at the end of the sequence." (source: Wikipedia)
IntArrayBag
methods should uphold them as a part of their postcondition- Methods work together to uphold the invariants
Running time analysis
Operation | Time Analysis |
---|---|
Constructor | O(c) |
add | O(1) |
addAll (w/o capacity increase) | O(n) |
addAll (w/ capacity increase) | O(n1 + n2) |
clone | O(c) |
countOccurrences | O(n) |
ensureCapacity | O(c) |
getCapacity | O(1) |
remove | O(n) |
size | O(1) |
trimToSize | O(n) |
union | O(c1 + c2) |
Linked lists
- A sequence of elements connected by links
- Elements are stored in nodes
IntNode
- Two private variables
data
link
Building the list
- If a link is
null
, signals end of list - Link nodes together in sequence
Accessor Methods
getData
setData
getLink
setLink
List Operations
- Add new nodes to the front
head
=new IntNode(5, head);
- Remove the front node
- Point
head
to next node in the list
- Point
- Search for a node
addAfterNode
removeNodeAfter
Traverse the list
listLength
listSearch
listPosition
Notice that these are all O(n).