[61B FA24] Lecture 10 - Inheritance 3_ Iterators, Object Methods
[61B FA24] Lecture 10 - Inheritance 3_ Iterators, Object Methods
Lecture 10 (Inheritance 3)
Object
Methods
CS61B, Fall 2024 @ UC Berkeley
Slides credit: Josh Hug
1
Today’s Goal: ArraySet
Iteration
• The Enhanced For Loop
• iterator, next, hasNext
• iterator, next, hasNext for
ArraySet
• Iterable
The enhanced for loop works by first calling the .iterator method of
the object.
● This returns an object of type Iterator<Integer>.
● The Iterator interface has its own API for fetching values one-by-
one:
○ hasNext: Tells us whether there are more values.
○ next: gets the
ArraySet<Integer> next
aset value.
= new
ArraySet<>();
aset.add(5);
aset.add(23);
aset.add(42); What is
for (int i : aset) { missing?
System.out.println(i);
}
How Iteration Really Works
javaset: 5 23 42
Iterator<Integer> seer
= javaset.iterator();
while (seer.hasNext()) {
System.out.println(seer.next());
How Iterators Work
$ java
IteratorDemo.java
javaset: 5 23 42
Iterator<Integer> seer
= javaset.iterator();
while (seer.hasNext()) {
System.out.println(seer.next());
How Iterators Work
$ java
IteratorDemo.java
javaset: 5 23 42
Iterator<Integer> seer
= javaset.iterator();
while (seer.hasNext()) {
System.out.println(seer.next());
How Iterators Work
javaset: 5 23 42
Iterator<Integer> seer
= javaset.iterator();
while (seer.hasNext()) {
System.out.println(seer.next());
How Iterators Work
javaset: 5 23 42
Iterator<Integer> seer
= javaset.iterator();
while (seer.hasNext()) {
System.out.println(seer.next());
How Iterators Work
Iterator<Integer> seer
= javaset.iterator();
while (seer.hasNext()) {
System.out.println(seer.next());
How Iterators Work
Iterator<Integer> seer
= javaset.iterator();
while (seer.hasNext()) {
System.out.println(seer.next());
How Iterators Work
Iterator<Integer> seer
= javaset.iterator();
while (seer.hasNext()) {
System.out.println(seer.next());
How Iterators Work
Iterator<Integer> seer
= javaset.iterator();
while (seer.hasNext()) {
System.out.println(seer.next());
Today’s Goal: ArraySet
Iteration
• The Enhanced For Loop
• iterator, next, hasNext
• iterator, next, hasNext
for ArraySet
iterator, next, • Iterable
The secret: The code on the left is just shorthand for the code on the
right. For code on right to compile, which checks does the compiler
need to do?
A. Does the Set interface have an iterator() method?
B. Does the Set interface have next/hasNext() methods?
C. Does the Iterator interface have an iterator method?
D. Does the Iterator interface have next/hasNext() methods?
Set<Integer> javaset = new
HashSet<Integer>();
Iterator<Integer> seer
for (int x : javaset) { = javaset.iterator();
System.out.println(x); while (seer.hasNext()) {
}
System.out.println(seer.next());
The Secret of the Enhanced For Loop
The secret: The code on the left is just shorthand for the code on the
right. For code on right to compile, which checks does the compiler
need to do?
A. Does the Set interface have an iterator() method?
B. Does the Set interface have next/hasNext() methods?
C. Does the Iterator interface have an iterator method?
D. Does the Iterator interface have next/hasNext() methods?
Set<Integer> javaset = new
HashSet<Integer>();
Iterator<Integer> seer
for (int x : javaset) { = javaset.iterator();
System.out.println(x); while (seer.hasNext()) {
}
System.out.println(seer.next());
Supporting Ugly Iteration in ArraySets
System.out.println(aseer.next());
Completed ArraySet iterator Method
Our code now supports “ugly” iteration, but enhanced for loop still
doesn’t work.
The problem: Java isn’t smart enough to realize that our ArraySet has
an iterator() method.
● Luckily there’s an interface for that.
ArraySet<Integer> aset = new
ArraySet<>();
aset.add(5);
aset.add(23);
aset.add(42); $ javac IterationDemo
for (int i : aset) { error: for-each not applicable to expression
System.out.println(i); type
} for (int i : aset) {
^
required: array or java.lang.Iterable
found: ArraySet<Integer>
For-each Iteration And ArraySets
Iterable<T>
System.out.println(javaset);
$ java
JavaSetPrintDemo
[5, 23, 42]
toString()
@Override
public String toString() {
StringBuilder returnSB = new
StringBuilder("{");
for (int i = 0; i < size; i += 1) {
returnSB.append(items[i]);
returnSB.append(", ");
}
returnSB.append("}");
return returnSB.toString();
}
Today’s Goal: ArraySet
Iteration
• The Enhanced For Loop
• iterator, next, hasNext
• iterator, next, hasNext for
ArraySet
• Iterable
Object Methods
• toString
== vs. equals • == vs. equals
• Better toString (Bonus)
Lecture 10, CS61B, Fall 2024 • .of (Bonus)
Object Methods
Naturally, can also use this to access your own instance variables or
methods.
● Unlike Python, where self is mandatory, using this is not
mandatory.
● Two code snippets below are exactly identical in behavior.
Naturally, can also use this to access your own instance variables or
methods.
● Unlike Python, where self is mandatory, using this is not
mandatory.
● If there’s ever a name conflict where a local variable has the same
name as an instance variable (left), you must use this if you want
to access the instance variable.
public Dog(int size) { public Dog(int s) {
size = size; size = s;
} Does nothing!
} Works correctly!
Below, we see the actual code for the default equals method in
Object.java.
● Code available here if you want to poke around:
https://github.com/openjdk/jdk17/blob/master/src/java.base/share/cl
asses/java/lang/Object.java#L162
...
System.out.println(aset);
$ java
EqualsDemo
ArraySet<Integer> aset2 = new False
ArraySet<>();
aset2.add(5); Returns false because
the default
aset2.add(23); implementation of equals
aset2.add(42); just uses ==.
System.out.println(aset.equals(aset2));
instanceOf Demo
The code below is pretty close to what a standard equals method looks
like.
@Override
public boolean equals(Object other) { Technically a raw type
if (this == other) { return true; } without a type
placeholder like
ArraySet<T>, but
if (other instanceof ArraySet otherSet) { don't worry about it.
if (this.size != otherSet.size) { return false; }
for (T x : this) {
if (!otherSet.contains(x)) {
return false;
}
}
return true;
}
return false;
}
ArraySet equals
The code below is pretty close to what a standard equals method looks
like.
@Override
public boolean equals(Object other) { Doesn't affect
if (this == other) { return true; } correctness, but saves
us time if this and
other reference the
if (other instanceof ArraySet otherSet) { same object.
if (this.size != otherSet.size) { return false; }
for (T x : this) {
if (!otherSet.contains(x)) {
return false;
}
}
return true;
}
return false;
}
Summary