[reset-listiterator] reset listiterator (#15645)

This commit is contained in:
Kai Yuan 2024-01-20 16:59:41 +01:00 committed by GitHub
parent 8fa22f186f
commit 900ff584fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 46 additions and 0 deletions

View File

@ -0,0 +1,46 @@
package com.baeldung.resetlistiterator;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.ListIterator;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
public class ResetListIteratorUnitTest {
private static final List<String> MY_LIST = List.of("A", "B", "C", "D", "E", "F", "G");
@Test
void whenRecreateAnListIterator_thenGetTheExpectedResult() {
ListIterator<String> lit = MY_LIST.listIterator();
lit.next();
lit.next();
lit.next();
lit.next();
lit = MY_LIST.listIterator();
assertFalse(lit.hasPrevious());
assertEquals("A", lit.next());
}
@Test
void whenBackwardIterationToTheFirst_thenGetTheExpectedResult() {
ListIterator<String> lit = MY_LIST.listIterator();
lit.next();
lit.next();
lit.next();
lit.next();
while (lit.hasPrevious()) {
lit.previous();
}
assertFalse(lit.hasPrevious());
assertEquals("A", lit.next());
}
}