Giao diện Java ListIterator

Trong hướng dẫn này, chúng ta sẽ tìm hiểu về giao diện Java ListIterator với sự trợ giúp của một ví dụ.

Các ListIteratorgiao diện của khuôn khổ bộ sưu tập Java cung cấp các chức năng để các yếu tố tiếp cận của một danh sách.

Nó là hai chiều. Điều này có nghĩa là nó cho phép chúng ta lặp lại các phần tử của một danh sách theo cả hai hướng.

Nó mở rộng Iteratorgiao diện.

Các Listgiao diện cung cấp một listIterator()phương thức trả về một thể hiện của ListIteratorgiao diện.

Các phương thức của ListIterator

Các ListIteratorgiao diện cung cấp phương pháp có thể được sử dụng để thực hiện các hoạt động khác nhau trên các yếu tố của một danh sách.

  • hasNext() – returns true if there exists an element in the list
  • next() – returns the next element of the list
  • nextIndex() returns the index of the element that the next() method will return
  • previous() – returns the previous element of the list
  • previousIndex() – returns the index of the element that the previous() method will return
  • remove() – removes the element returned by either next() or previous()
  • set() – replaces the element returned by either next() or previous() with the specified element

Ví dụ 1: Triển khai ListIterator

Trong ví dụ dưới đây, chúng tôi đã triển khai next()nextIndex()và hasNext()phương pháp của ListIteratorgiao diện trong một array list.

import java.util.ArrayList;
import java.util.ListIterator;

class Main {
    public static void main(String[] args) {
        // Creating an ArrayList
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(3);
        numbers.add(2);
        System.out.println("ArrayList: " + numbers);

        // Creating an instance of ListIterator
        ListIterator<Integer> 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());
    }
}

Đầu ra

ArrayList: [1, 3, 2]
Next Element: 1
Position of Next Element: 1
Is there any next element? true

Ví dụ 2: Triển khai ListIterator

Trong ví dụ dưới đây, chúng tôi đã triển khai các phương thức previous()và giao diện trong một danh sách mảng.previousIndex()ListIterator

import java.util.ArrayList;
import java.util.ListIterator;

class Main {
    public static void main(String[] args) {
        // Creating an ArrayList
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(3);
        numbers.add(2);
        System.out.println("ArrayList: " + numbers);

        // Creating an instance of ListIterator
        ListIterator<Integer> 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);
    }
}

Đầu ra

ArrayList: [1, 3, 2]
Previous Element: 3
Position of the Previous Element: 0

Trong ví dụ trên, ban đầu, thể hiện của Iteratorlà trước 1. Vì không có phần tử nào trước 1 nên việc gọi previous()phương thức sẽ ném ra một ngoại lệ.

Sau đó chúng tôi sử dụng các next()phương pháp 2 lần. Bây giờ Iteratorphiên bản sẽ nằm trong khoảng từ 3 đến 2.

Do đó, previous()phương thức trả về 3.









Gõ tìm kiếm nhanh...