Difference between revisions of "Iterable Immutable List Assignment"
Jump to navigation
Jump to search
(→Test) |
|||
Line 42: | Line 42: | ||
=Diagrams= | =Diagrams= | ||
==ImmutableList== | ==ImmutableList== | ||
− | <nowiki> | + | <nowiki>ImList<Char> letters = Lists.brackets('A', 'B', 'C'); </nowiki> |
[[File:ImmutableList_letters.svg|600px]] | [[File:ImmutableList_letters.svg|600px]] | ||
Line 48: | Line 48: | ||
==Initial Iterator State== | ==Initial Iterator State== | ||
− | <nowiki> | + | <nowiki>ImList<Char> letters = Lists.brackets('A', 'B', 'C'); |
Iterator<Char> iter = letters.iterator(); </nowiki> | Iterator<Char> iter = letters.iterator(); </nowiki> | ||
Line 55: | Line 55: | ||
==Iterator State after next()== | ==Iterator State after next()== | ||
− | <nowiki> | + | <nowiki>ImList<Char> letters = Lists.brackets('A', 'B', 'C'); |
Iterator<Char> iter = letters.iterator(); | Iterator<Char> iter = letters.iterator(); | ||
if(iter.hasNext()) { | if(iter.hasNext()) { | ||
Line 64: | Line 64: | ||
==Iterator State after next() next()== | ==Iterator State after next() next()== | ||
− | <nowiki> | + | <nowiki>ImList<Char> letters = Lists.brackets('A', 'B', 'C'); |
Iterator<Char> iter = letters.iterator(); | Iterator<Char> iter = letters.iterator(); | ||
if(iter.hasNext()) { | if(iter.hasNext()) { | ||
Line 75: | Line 75: | ||
[[File:ImmutableList_iterator_next_next.svg|600px]] | [[File:ImmutableList_iterator_next_next.svg|600px]] | ||
==Iterator State after next() next() next()== | ==Iterator State after next() next() next()== | ||
− | <nowiki> | + | <nowiki>ImList<Char> letters = Lists.brackets('A', 'B', 'C'); |
Iterator<Char> iter = letters.iterator(); | Iterator<Char> iter = letters.iterator(); | ||
if(iter.hasNext()) { | if(iter.hasNext()) { |
Latest revision as of 19:05, 24 April 2022
Exercise To Revisit
In this studio we will complete the EmptyImmutableList
and NonEmptyImmutableList
implementations from the ImmutableList exercise.
ImmutableList
The ImmutableList interface extends Iterable which has one method: iterator().
public interface ImmutableList<E> extends Iterable<E> { E head(); ImmutableList<E> tail(); boolean isEmpty(); }
Iterable
interface java.lang.Iterable<T>
Iterator
interface java.util.Iterator<T>
Examples
Using Iterable
Implementing Iterable: DOM NodeList
Example
class: | IterableImmutableListExample.java | ![]() |
package: | immutable.list.example | |
source folder: | src/main/java |
The code:
ImmutableList<Integer> numbers = Lists.brackets(4, 66, 99); for (Integer i : numbers) { System.out.println(i); }
produces:
4 66 99
Diagrams
ImmutableList
ImList<Char> letters = Lists.brackets('A', 'B', 'C');
Initial Iterator State
ImList<Char> letters = Lists.brackets('A', 'B', 'C'); Iterator<Char> iter = letters.iterator();
Iterator State after next()
ImList<Char> letters = Lists.brackets('A', 'B', 'C'); Iterator<Char> iter = letters.iterator(); if(iter.hasNext()) { char a = iter.next(); }
Iterator State after next() next()
ImList<Char> letters = Lists.brackets('A', 'B', 'C'); Iterator<Char> iter = letters.iterator(); if(iter.hasNext()) { char a = iter.next(); if(iter.hasNext()) { char b = iter.next(); } }
Iterator State after next() next() next()
ImList<Char> letters = Lists.brackets('A', 'B', 'C'); Iterator<Char> iter = letters.iterator(); if(iter.hasNext()) { char a = iter.next(); if(iter.hasNext()) { char b = iter.next(); if(iter.hasNext()) { char c = iter.next(); } } }
Code To Implement
enum EmptyImmutableList
class: | EmptyImmutableList.java | ![]() |
methods: | iterator | |
package: | immutable.list.assignment | |
source folder: | src/main/java |
iterator()
return an Iterator for the EmptyImmutableList.
Question to ask yourself: Does an iterator for an EmptyImmutableList ever have a next?
class NonEmptyImmutableList
class: | NonEmptyImmutableList.java | ![]() |
methods: | iterator | |
package: | immutable.list.assignment | |
source folder: | src/main/java |
iterator()
return an Iterator for this instance of NonEmptyImmutableList.
Note: If you are within an anonymous inner class, the way to get the outer class's this instance is to qualify it with the out class identifier. For example:
NonEmptyImmutableList.this
Test
class: | IterableAssignmentTestSuite.java | ![]() |
package: | immutable.list.util.exercise | |
source folder: | src/test/java |