CS 61B part4 Iterators, Exceptions
prev:[[61B_Part_3_OOP]]
next: [[Git-intro]]
Hug61B chapter6
sets
a collection of unique elements.
you can only have one copy of each element. There is also no sense of order.
Java set example use:
1 | Set<String> s = new HashSet<>(); |
Goal: make our own ArraySet, with the following methods:
add(value): add the value to the set if not already present
contains(value): check to see if ArraySet contains the key
size(): return number of values
our code:
1 | import java.util.Iterator; |
Expectations
Small error: when null was added to the set, the nullPointerException was raised.
So, we would throw expectations when null was added.
In Java, Exceptions are objects, and we throw exceptions using the following format:
throw new ExceptionObject(parameter1, ...)
the following code would be added:
1 | if (x == null) { |
Why this one is better?
- we have control of our own code
- more useful error messages were got.
Also, we can check if it is null to completely avoid the error.
Iteration
we can do this with Java’s HashSet
1 | Set<String> s = new HashSet<>(); |
Why with our ArraySet, there is some questions?
The nature of Enhanced For Loops
The for loops above can be translated as the Iterator version:
1 | Set<String> s = new HashSet<>(); |
Iterator<String> seer = s.iterator();: build a new Iterator
seer.hasNext(): whether the Iterator is still have items left
seer.next(): return the next item and push the iterator by one item.
Implementing Iterators
Based on the implementations above,
the List interface have an iterator() method.
1 | public interface Iterable<T> { |
also, the Iterator interface have next/hasNext() methods.
1 | public interface Iterator<T> { |
To implement it, we first write a new class called ArraySetIterator, nested inside ArraySet:
1 | private class ArraySetIterator implements Iterator<T> { |
Afterwards, we need to make ArraySet implement the Iterable interface.
1 | public Iterator<T> iterator() { |
Iterable: the interface that makes a class able to be iterated on
Iterator: the interface that defines the object with methods to actually do that iteration.
Object methods
the list of object methods
1 | String toString() |
we would explain the first two methods.
toString()
provides a string representation of an object
System.out.println(dog); Actually run like this:
1 | String s = dog.toString(); |
Default: print the location of the object in memory(hexadecimal(十六进制的) string)
Classes like Arraylist and java arrays have their own overridden versions of the toString() method.
Therefore, if we want to print our class in a readable format, Override toString() is necessary.
The overall solution of ArraySet is here.
possible solution 1:
1 | public String toString() { |
Its efficiency is very low!
when you use string concatenation in Java like so: returnString += keys[i]
you are actually not just appending to returnString, you are creating an entirely new string.
and it takes time.
To solve this problem, Stringbuilder in Java helps to build the mutable string.
1 | public String toString() { |
equals()
In Java, == checks if the two objects are actually the same object in the memory.
If they are addresses, it means checking if the address is equal
However, equals() method, in default, checks the same as ==. However, we can override the method to
define whatever we want!
1 | public boolean equals(Object other) { |
Rules for Equals in Java
- equals must be an equivalence relation
- reflexive:
x.equals(x)is true - symmetric:
x.equals(y)if and only ify.equals(x) - transitive:
x.equals(y)andy.equals(z)impliesx.equals(z)
- It must take an Object argument, in order to override the original
.equals()method - It must be consistent if
x.equals(y), then as long asxandyremain unchanged:xmust continue to equaly - It is never true for null
x.equals(null)must be false
