Difference between revisions of "Iterable Immutable List Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
Line 44: Line 44:
  
 
=Code To Implement=
 
=Code To Implement=
 +
==enum EmptyImmutableList==
 
{{JavaToImplement|EmptyImmutableList|iterator|immutable.list.assignment}}
 
{{JavaToImplement|EmptyImmutableList|iterator|immutable.list.assignment}}
 +
===iterator()===
  
 +
==class NonEmptyImmutableList==
 
{{JavaToImplement|NonEmptyImmutableList|iterator|immutable.list.assignment}}
 
{{JavaToImplement|NonEmptyImmutableList|iterator|immutable.list.assignment}}
 +
===iterator()===
  
 
=Test=
 
=Test=

Revision as of 08:15, 14 December 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()

Examples

Using Iterable

Implementing Iterable

DoubleRange

ArrayIterable

Example

class: IterableImmutableListExample.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

Code To Implement

enum EmptyImmutableList

class: EmptyImmutableList.java Java.png
methods: iterator
package: immutable.list.assignment
source folder: src/main/java

iterator()

class NonEmptyImmutableList

class: NonEmptyImmutableList.java Java.png
methods: iterator
package: immutable.list.assignment
source folder: src/main/java

iterator()

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