You are on page 1of 24

ArraySet Methods and ArrayIterator

ArraySet Methods with Big-O analysis ArrayIterator Class ArrayIterator Methods ArrayIterator Summary Reading: L&C 15.3, Pages 40-41

ArraySet Methods
An interface cant define any constructor methods, but any implementing class needs to have one or more of them (maybe overloading the constructor) Default Contructor:
public ArraySet() { this(DEFAULT_CAPACITY); } // must be 1st statement // call other constructor // with default capacity

Constructor with a specified initial capacity:


public ArraySet(int initialCapacity) { count = 0; contents = (T[]) new Object[initialCapacity]; }

ArraySet Methods
size - O(1)
public int size() { return count; }

isEmpty O(1)
public boolean isEmpty() { return count == 0; }

ArraySet Methods
add O(n)
public void add (T element) { if (!contains(element)) { if (size() == contents.length) expandCapacity(); contents[count++] = element; } }

ArraySet Methods
contains O(n)
public boolean contains(T target) { for (T element : contents) if (target.equals(element)) return true; return false; }

Why not use following if condition above?


if (element.equals(target))

ArraySet Methods
expandCapacity O(n)
private void expandCapacity() { T[] larger = (T[]) new Object[2 * contents.length]; for (int i = 0; i < contents.length; i++) larger[i] = contents[i]; contents = larger; } // original array // becomes garbage

ArraySet Methods
addAll O(n2) (Note: This is much simpler to code using a for-each loop because we added extends Iterable to SetADT interface definition)
public void addAll(setADT<T> set) { for(T element : set) add(element); }

ArraySet Methods
remove O(n)
public T remove(T target) throws EmptySetException, NoSuchElementException { if (isEmpty()) throw new EmptySetException(); for (int i = 0; i < count; i++) { if (contents[i].equals(target)) { T result = contents[i]; contents[i] = contents[--count]; contents[count] = null; return result; } } throw new NoSuchElementException(); }

ArraySet Methods
removeRandom (Authors) O(1)
public T removeRandom() throws EmptySetException { if (isEmpty()) throw new EmptySetException(); int choice = rand.nextInt(count); T result = contents[choice]; contents[choice] = contents[--count]; contents[count] = null; return result; }

ArraySet Methods
removeRandom (Bad Alternative?)
Avoids duplicating some code in the remove method Throws any EmptySetException that occurs in remove NoSuchElementException can not occur in remove (contents[0 <= index < count] is an element in the set) BUT, this alternative algorithm is O(n) instead of O(1)! public T removeRandom() throws EmptySetException { return remove(contents[rand.nextInt(count)]); }

ArraySet Methods
union O(n2)
public SetADT<T> union (SetADT<T> set) { ArraySet<T> both = new ArraySet<T>(); both.addAll(this); both.addAll(set); return both; }
11

ArraySet Methods
equals O(n2)
public boolean equals (SetADT<T> set) { if (size() != set.size()) return false; // cant be equal ArraySet<T> temp1 = new ARRAYSET<T>(); ArraySet<T> temp2 = new ARRAYSET<T>(); temp1.addAll(this); temp2.addAll(set);
12

ArraySet Methods
for (T obj : set) { if (temp1.contains(obj)) { temp1.remove(obj); temp2.remove(obj); } } return temp1.isEmpty() && temp2.isEmpty(); }// end equals method
13

ArraySet Methods
toString O(n)
public String toString() { String result = ; for (T obj : contents) { if (obj == null) // first null is at count return result; result += obj + \n; } return result; // exactly full no nulls }

14

ArraySet Methods
iterator O(1)
public Iterator<T> iterator() { return new ArrayIterator<T>(contents, count); }

We need to study the ArrayIterator class to understand how to implement an Iterator


15

ArrayIterator class
We may have several collection classes like the ArraySet class that use an array as an underlying data structure We would like to reuse one Iterator class for all of these collection classes So we generalize the problem of scanning using ArrayIterator class with two methods:
boolean hasNext() T next()
16

ArrayIterator Class
The iterator method of the ArraySet class instantiates and returns a reference to a new ArrayIterator object to its caller The ArrayIterator constructor needs to get a reference to the array and a count of the number of valid elements in the array (may be less than or equal to length attribute)

17

ArrayIterator Definition/Attributes
Class Definition/Attribute Declarations
public class ArrayIterator<T> implements Iterator<T> { private int count; private int current; private T[] items; ... }
18

ArrayIterator Methods
Default constructor should NOT be used May want to include this code to prevent any calls to the default constructor:
private ArrayIterator() { }

Overloaded Constructor:
public ArrayIterator(T[] collection, int count) { items = collection; // NOTE: creates an alias // not a new array this.count = count; current = 0; }
19

ArrayIterator Methods
In the attributes and constructor, we do not use the array length attribute We want to use the parameter count not collection.length because the collection array may not be full collection array may be populated only from 0 to count-1 If the array being passed is full, the caller can pass array.length value in count
20

ArrayIterator Methods
hasNext O(1)
public boolean hasNext() { return current < count; }

next O(1)
public T next() { if (!hasNext()) throw new NoSuchElementException(); return items[current++]; }
21

ArrayIterator Methods
remove O(1) We dont need to implement real code for the remove method, but there is no return value that we can use to indicate that it is not implemented If we dont implement it, we indicate that the code is not implemented by throwing an exception
public void remove() throws UnsupportedOperationException { throw new UnsupportedOperationException(); }
22

ArrayIterator Methods
If we do implement the remove method, notice that we dont specify the element that is to be removed and we do not return a reference to the element being removed It is assumed that the calling code has been iterating on condition hasNext() and calling next() and already has a reference The last element returned by next() is the element that will be removed
23

ArrayIterator Method Analysis


All three ArrayIterator methods are O(1) However, they are usually called inside an external while loop or for-each loop Hence, the process of iterating through a collection using an Iterator is O(n)

24

You might also like