W tym samouczku dowiemy się o interfejsie Java ListIterator na przykładzie.
ListIterator
Interfejs zbiorach ramach Java zapewnia funkcjonalność elementów dostępowych z listy.
Jest dwukierunkowy. Oznacza to, że pozwala nam iterować elementy listy w obu kierunkach.
Rozszerza Iterator
interfejs.
List
Interfejs zapewnia listIterator()
metody zwracającej wystąpienie ListIterator
interfejsu.
Metody ListIterator
ListIterator
Interfejs zapewnia metody, które mogą być używane do wykonywania różnych operacji na elementach listy.
hasNext()
- zwraca prawdę, jeśli istnieje element na liścienext()
- zwraca następny element listynextIndex()
zwraca indeks elementu, którynext()
zwróci metodaprevious()
- zwraca poprzedni element listypreviousIndex()
- zwraca indeks elementu, któryprevious()
zwróci metodaremove()
- usuwa element zwracany przez albonext()
alboprevious()
set()
- zastępuje element zwrócony przez jedennext()
lubprevious()
określony element
Przykład 1: Implementacja ListIterator
W poniższym przykładzie, wdrożyliśmy next()
, nextIndex()
oraz hasNext()
metod ListIterator
interfejsu w liście tablicy.
import java.util.ArrayList; import java.util.ListIterator; class Main ( public static void main(String() args) ( // Creating an ArrayList ArrayList numbers = new ArrayList(); numbers.add(1); numbers.add(3); numbers.add(2); System.out.println("ArrayList: " + numbers); // Creating an instance of ListIterator ListIterator iterate = numbers.listIterator(); // Using the next() method int number1 = iterate.next(); System.out.println("Next Element: " + number1); // Using the nextIndex() int index1 = iterate.nextIndex(); System.out.println("Position of Next Element: " + index1); // Using the hasNext() method System.out.println("Is there any next element? " + iterate.hasNext()); ) )
Wynik
ArrayList: (1, 3, 2) Następny element: 1 Pozycja następnego elementu: 1 Czy jest jakiś następny element? prawdziwe
Przykład 2: Implementacja ListIterator
W poniższym przykładzie zaimplementowaliśmy metody previous()
i interfejsu na liście tablic.previousIndex()
ListIterator
import java.util.ArrayList; import java.util.ListIterator; class Main ( public static void main(String() args) ( // Creating an ArrayList ArrayList numbers = new ArrayList(); numbers.add(1); numbers.add(3); numbers.add(2); System.out.println("ArrayList: " + numbers); // Creating an instance of ListIterator ListIterator iterate = numbers.listIterator(); iterate.next(); iterate.next(); // Using the previous() method int number1 = iterate.previous(); System.out.println("Previous Element: " + number1); // Using the previousIndex() int index1 = iterate.previousIndex(); System.out.println("Position of the Previous element: " + index1); ) )
Wynik
ArrayList: (1, 3, 2) Poprzedni element: 3 Pozycja poprzedniego elementu: 0
W powyższym przykładzie początkowo instancja Iterator
była przed 1. Ponieważ nie było elementu przed 1, więc wywołanie previous()
metody zgłosi wyjątek.
Następnie zastosowaliśmy te next()
metody 2 razy. Teraz Iterator
instancja będzie miała od 3 do 2.
W związku z tym previous()
metoda zwraca 3.