Home CS61B: Lecture 10
Post
Cancel

CS61B: Lecture 10

Iterators, Object Methods

  • We want to implement a Set called ArraySet
    • Sets can only have one copy of each item.
  • We start with an ArraySet with the following methods:
    • add(value)
    • contains(value)
    • get(value)
  • When we want to check equality between items, we use .equals() rather than ==
    • == only checks if the bits are the same.

Enanced forloop

  • This is a shorthand notation but not exactly method overloading.
  • We must implement this using iterators.
  • We may only use the enhanced forloop using the iterator object.
1
2
3
4
5
6
7
Set<Integer> javaset = new HashSet<Integer>();
javaset.add(5);
javaset.add(23);
javaset.add(42);
for (int i : javaset) {
    System.out.println(i);
}
1
2
3
5
23
42
  • The enhanced forloop first calls the .iterator() method of the object.
    • We then get an object of type Iterator.
    • The Iterator interface has it’s own interface that allows us to check if there is a next value hasNext() or get the next value next()
  • The enhanced for loop is really doing the following code:
1
2
3
4
5
6
7
8
9
Set<Integer> javaset = new HashSet<Integer>();
javaset.add(5);
javaset.add(23);
javaset.add(42);

Iterator<Integer> seer = javaset.iterator();
while (seer.hasNext()) {
    System.out.println(seer.next());
}
1
2
3
5
23
42

Iterators

  • To support ugly iteration, we need to add an iterator() method to ArraySet that returns an Iterator<T>
  • The Iterator<T> that we return must have a .hasNext() and a next() method.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// an arraySetItereator is the seer
private class ArraySetIterator implements Iterator<T> {
    private int pos;

    public ArraySetIterator () {
        pos = 0;
    }

    @Override
    public boolean hasNext() {
        if (pos < size) {
            return true;
        }
        return false;
    }

    @Override
    public T next() {
        T itemToReturn = items[pos];
        pos++;
        return itemToReturn;
    }
}

public Iterator<T> iterator() {
    return new ArraySetIterator();
}
  • To allow for the enhanced for-each loop, we must ensure our ArraySet is also an iterable object.
1
2
3
public class ArraySet<T> implements Iterable<T> {
    ...
}

Object class

  • All classes and objects in Java are hyponyms of Object.
  • One of the methods of an Object is toString, which will return a string representation of our object.
1
2
3
4
5
6
7
8
9
10
@Override
public String toString() {
    StringBuilder stringToReturn = "{";
    for (T x : this) {
        stringToReturn.append(x);
        stringToReturn.append(",");
    }
    stringToReturn.append("}");
    return stringToReturn.toString();
}

== vs .equals()

  • == compares the bits. So == means “referencing the same object.”
  • .equals() comapres the values, but we typically have to rewrite the .equals() method for our class.
    • .equals() by default behaves the same as ==. It checks the addresses.
  • However, .equals() must accept some Object o, so most time we need to check if o is the object that we want to compare to.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Override
public boolean equals(Object o) {
    if (this == o) {return true;}
    if (o instanceof ArraySet someSet) {
        if (this.size != someSet.size) {
            return false;
        }
        for (T x : this) {
            if (!someSet.contains(x)) {
                return false;
            }
        }
        return true;
    }
    return false;
}
This post is licensed under CC BY 4.0 by the author.

CS61B: Lecture 9

CS61B: Lecture 11