Difference between revisions of "Iterable Immutable List Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
Line 20: Line 20:
 
#[https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html#next-- T next()]
 
#[https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html#next-- T next()]
  
=Code To Implement=
+
=Example=
==DefaultImmutableList==
+
==DoubleRange==
{{JavaToImplement|DefaultImmutableList|iterator|immutable.list.assignment}}
+
The [[DoubleRange]] warm up to gain experience implementing an Iterable<T> without simply containing a List<T> which holds the values. Further, you can gain experience creating instances of an anonymous class.
===iterator===
+
 
<nowiki>public Iterator<E> iterator()</nowiki>
+
<youtube>Dww-_5EiwA8</youtube>
  
 
=Example=
 
=Example=

Revision as of 18:53, 24 April 2020

Studio To Revisit

In this studio we will complete the DefaultImmutableList implementation from the ImmutableList studio.

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>

  1. Iterator<T> iterator()

Iterator

interface java.util.Iterator<T>

  1. boolean hasNext()
  2. T next()

Example

DoubleRange

The DoubleRange warm up to gain experience implementing an Iterable<T> without simply containing a List<T> which holds the values. Further, you can gain experience creating instances of an anonymous class.

Example

class: Mary Berry.java Presentation icon-72a7cf.svg
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

Test

class: IterableAssignmentTestSuite.java Junit.png
package: immutable.list.assignment
source folder: src/test/java
class: ImmutableListIteratorMethodTestSuite.java Junit.png
package: immutable.list.assignment
source folder: src/test/java