Interfejs Java ListIterator

Spisie treści

W tym samouczku dowiemy się o interfejsie Java ListIterator na przykładzie.

ListIteratorInterfejs 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 Iteratorinterfejs.

ListInterfejs zapewnia listIterator()metody zwracającej wystąpienie ListIteratorinterfejsu.

Metody ListIterator

ListIteratorInterfejs 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ście
  • next() - zwraca następny element listy
  • nextIndex()zwraca indeks elementu, który next()zwróci metoda
  • previous() - zwraca poprzedni element listy
  • previousIndex()- zwraca indeks elementu, który previous()zwróci metoda
  • remove()- usuwa element zwracany przez albo next()alboprevious()
  • set()- zastępuje element zwrócony przez jeden next()lub previous()określony element

Przykład 1: Implementacja ListIterator

W poniższym przykładzie, wdrożyliśmy next(), nextIndex()oraz hasNext()metod ListIteratorinterfejsu 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 Iteratorbył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 Iteratorinstancja będzie miała od 3 do 2.

W związku z tym previous()metoda zwraca 3.

Interesujące artykuły...