Skip to content

Commit 0a4687f

Browse files
committed
NestedIterator: adjust javadoc, add tests
1 parent dd94ec0 commit 0a4687f

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/main/java/by/andd3dfx/iterators/NestedIterator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
* <pre>
1111
* <a href="https://leetcode.com/problems/flatten-nested-list-iterator/description/">Task description</a>
1212
*
13-
* You are given a nested list of integers nestedList. Each element is either an integer or a list whose elements may also be integers or other lists. Implement an iterator to flatten it.
13+
* You are given a nested list of integers nestedList. Each element is either an integer or a list whose elements may also be integers
14+
* or other lists. Implement an iterator to flatten it.
1415
*
1516
* Implement the NestedIterator class:
16-
*
1717
* NestedIterator(List<NestedInteger> nestedList) Initializes the iterator with the nested list nestedList.
1818
* int next() Returns the next integer in the nested list.
1919
* boolean hasNext() Returns true if there are still some integers in the nested list and false otherwise.

src/test/java/by/andd3dfx/iterators/NestedIteratorTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.ArrayList;
77
import java.util.Arrays;
88
import java.util.List;
9+
import java.util.NoSuchElementException;
910
import org.junit.Test;
1011

1112
public class NestedIteratorTest {
@@ -24,13 +25,22 @@ public void testNextAndHasNext() {
2425
new NestedIntegerList(new NestedInteger(4), new NestedIntegerList(6))
2526
),
2627
List.of(1, 4, 6));
28+
}
2729

30+
@Test
31+
public void testNextAndHasNext_emptyList() {
2832
check(List.of(
2933
new NestedIntegerList()
3034
),
3135
List.of());
3236
}
3337

38+
@Test(expected = NoSuchElementException.class)
39+
public void testNextWhenNoElements() {
40+
var nestedIterator = new NestedIterator(List.of());
41+
nestedIterator.next();
42+
}
43+
3444
private void check(List<INestedInteger> incoming, List<Integer> outgoing) {
3545
var result = new ArrayList<>();
3646

0 commit comments

Comments
 (0)