Can we use iterator in list Java? Chi tiết

Thủ Thuật Hướng dẫn Can we use iterator in list Java? Chi Tiết

Bạn đang tìm kiếm từ khóa Can we use iterator in list Java? được Update vào lúc : 2022-01-23 04:37:13 . Với phương châm chia sẻ Bí quyết về trong nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi Read tài liệu vẫn ko hiểu thì hoàn toàn có thể lại phản hồi ở cuối bài để Ad lý giải và hướng dẫn lại nha.

In the last tutorial, we discussed Iterator in Java using which we can traverse a List or Set in forward direction. Here we will discuss ListIterator that allows us to traverse the list in both directions (forward and backward).

Nội dung chính

ListIterator Example

In this example we are traversing an ArrayList in both the directions.

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

public class ListIteratorExample
public static void main(String a[])
ListIterator<String> litr = null;
List<String> names = new ArrayList<String>();
names.add(“Shyam”);
names.add(“Rajat”);
names.add(“Paul”);
names.add(“Tom”);
names.add(“Kate”);
//Obtaining list iterator
litr=names.listIterator();

System.out.println(“Traversing the list in forward direction:”);
while(litr.hasNext())
System.out.println(litr.next());

System.out.println(“nTraversing the list in backward direction:”);
while(litr.hasPrevious())
System.out.println(litr.previous());

Output:

Traversing the list in forward direction:
Shyam
Rajat
Paul
Tom
Kate

Traversing the list in backward direction:
Kate
Tom
Paul
Rajat
Shyam

Note: We can use Iterator to traverse List and Set both but using ListIterator we can only traverse list. There are several other differences between Iterator and ListIterator, we will discuss them in next post.

Methods of ListIterator

1) void add(E e): Inserts the specified element into the list (optional operation).
2) boolean hasNext(): Returns true if this list iterator has more elements when traversing the list in the forward direction.
3) boolean hasPrevious(): Returns true if this list iterator has more elements when traversing the list in the reverse direction.
4) E next(): Returns the next element in the list and advances the cursor position.
5) int nextIndex(): Returns the index of the element that would be returned by a subsequent call to next().
6) E previous(): Returns the previous element in the list and moves the cursor position backwards.
7) int previousIndex(): Returns the index of the element that would be returned by a subsequent call to previous().
8) void remove(): Removes from the list the last element that was returned by next() or previous() (optional operation).
9) void set(E e): Replaces the last element returned by next() or previous() with the specified element (optional operation).

Reference:

ListIterator javadoc

Reply
1
0
Chia sẻ

Clip Can we use iterator in list Java? ?

Bạn vừa tìm hiểu thêm Post Với Một số hướng dẫn một cách rõ ràng hơn về Review Can we use iterator in list Java? tiên tiến và phát triển nhất

Chia Sẻ Link Tải Can we use iterator in list Java? miễn phí

Bạn đang tìm một số trong những Chia Sẻ Link Cập nhật Can we use iterator in list Java? Free.

Giải đáp vướng mắc về Can we use iterator in list Java?

Nếu Pro sau khi đọc nội dung bài viết Can we use iterator in list Java? , bạn vẫn chưa hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Mình lý giải và hướng dẫn lại nha
#iterator #list #Java

Exit mobile version