As we know Java has four cursors: Enumeration, Iterator, ListIterator, and Spliterator. We have already discussed Enumeration and Iterator cursors in my previous post. Before going through this post, please go through my previous post at: Java Iterator.
众所周知,Java有四个游标:Enumeration,Iterator,ListIterator和Spliterator。 在上一篇文章中,我们已经讨论了Enumeration和Iterator游标。 在阅读本文之前,请阅读我以前的文章: Java Iterator 。
In this post, we will discuss about third Java cursor: ListIterator.
在本文中,我们将讨论第三个Java游标: ListIterator 。
Like Iterator, ListIterator is a Java Iterator, which is used to iterate elements one-by-one from a List implemented object.
与Iterator一样,ListIterator是Java Iterator,用于从List实现的对象中逐个迭代元素。
NOTE:- What is CRUD operations in Collection API?
注意:-什么是Collection API中的CRUD操作?
In Java, ListIterator is an interface in Collection API. It extends Iterator interface. To support Forward and Backward Direction iteration and CRUD operations, it has the following methods. We can use this Iterator for all List implemented classes like ArrayList, CopyOnWriteArrayList, LinkedList, Stack, Vector, etc.
在Java中,ListIterator是Collection API中的接口。 它扩展了Iterator接口。 为了支持前进和后退方向迭代以及CRUD操作,它具有以下方法。 我们可以将此Iterator用于所有List实现的类,例如ArrayList,CopyOnWriteArrayList,LinkedList,Stack,Vector等。
We will explore these methods in-depth with some useful methods in the coming sections.
在接下来的部分中,我们将使用一些有用的方法来深入探讨这些方法。
Java ListIterator interface has the following Methods.
Java ListIterator接口具有以下方法。
We will explore these methods one-by-one with useful examples in the coming sections.
我们将在接下来的部分中逐一探讨这些方法,并提供有用的示例。
In this section, we will discuss some ListIterator methods with some examples. First, we need to understand about how to get this iterator object.
在本节中,我们将通过一些示例讨论一些ListIterator方法。 首先,我们需要了解如何获取此迭代器对象。
How to get ListIterator?
如何获得ListIterator?
ListIterator listIterator()
It returns a list iterator over the elements in this list.
它在此列表中的元素上返回列表迭代器。
Example:-
例:-
import java.util.*;
public class ListIteratorDemo
{
public static void main(String[] args)
{
List();
names.add("Rams");
names.add("Posa");
names.add("Chinni");
// Getting ListIterator
ListIterator
Output:-
输出:-
Rams
Posa
Chinni
In section, we will explore how ListIterator’s methods work to perform Forward Direction and Backward Direction iterations.
在本节中,我们将探索ListIterator的方法如何执行前向和后向迭代。
import java.util.*;
public class BiDirectinalListIteratorDemo
{
public static void main(String[] args)
{
List();
names.add("Rams");
names.add("Posa");
names.add("Chinni");
// Getting ListIterator
ListIterator
Output:-
输出:-
Forward Direction Iteration:
Rams
Posa
Chinni
Backward Direction Iteration:
Chinni
Posa
Rams
As we know Java has four cursors: Enumeration, Iterator, ListIterator, and Spliterator. We can categorize them into two main types as shown below:
众所周知,Java有四个游标:Enumeration,Iterator,ListIterator和Spliterator。 我们可以将它们分为两种主要类型,如下所示:
They are Cursors which supports only Forward Direction iterations. For instance, Enumeration, Iterator, etc. are Uni-Directional Iterators.
它们是仅支持正向迭代的游标。 例如,Enumeration,Iterator等是单向迭代器。
They are Cursors which supports Both Forward Direction and Backward Direction iterations. For instance, ListIterator is Bi-Directional Iterator.
它们是同时支持向前和向后迭代的游标。 例如,ListIterator是双向迭代器。
As we know, Java ListIterator works in both directions that mean it works in the forward direction as well as backward direction. It is a Bi-directional Iterator. To support this functionality, it has two sets of methods.
众所周知,Java ListIterator可以在两个方向上工作,这意味着它既可以在正向也可以在反向进行。 它是一个双向迭代器。 为了支持此功能,它具有两组方法。
We need to use the following methods to support Forward Direction Iteration:
我们需要使用以下方法来支持正向迭代:
We need to use the following methods to support Backward Direction Iteration:
我们需要使用以下方法来支持向后方向迭代:
In my previous post, we have already discussed how an Iterator works internally in forwarding Direction at “How Java Iterator Works Internally?” section. Even ListIterator also works in that same way. If you want go through my previous post, please click here: Java Iterator.
在我以前的文章中,我们已经在“ Java迭代器内部如何工作”的转发指导中讨论了迭代器如何在内部工作。 部分。 甚至ListIterator也以相同的方式工作。 如果您想浏览我以前的文章,请单击此处: Java Iterator 。
In this section, we will discuss how ListIterator works in Backward Direction.
在本节中,我们将讨论ListIterator如何在向后方向上工作。
Let us take the following LinkedList object to understand this functionality.
让我们采用以下LinkedList对象来了解此功能。
List names = new LinkedList<>();
names.add("E-1");
names.add("E-2");
names.add("E-3");
.
.
.
names.add("E-n");
Now create a ListIterator object on LinkedList as shown below:
现在,在LinkedList上创建一个ListIterator对象,如下所示:
ListIterator namesIterator = names.listLterator();
Let us assume “namesIterator” ListIterator looks like below:
让我们假设“ namesIterator” ListIterator如下所示:
Here ListIterator’s Cursor is pointing to the before the first element of the List. Now we run the following code snippet in the while loop.
这里ListIterator的Cursor指向List的第一个元素之前的。 现在,我们在while循环中运行以下代码段。
namesIterator.hasNext();
namesIterator.next();
When we run the above code snippet in the while loop, ListIterator’s Cursor points to the last element in the LinkedList.
当我们在while循环中运行上述代码片段时,ListIterator的Cursor指向LinkedList中的最后一个元素。
Then we can run the following code snippet to start traversing from the end to the start.
然后,我们可以运行以下代码段以开始从头开始遍历。
namesIterator.hasPrevious();
namesIterator.previous();
When we run the above code snippet, ListIterator’s Cursor points to the “Last but one” element in the List as shown in the above diagram. Do this process to reach the ListIterator’s Cursor to the first element of the LinkedList.
当我们运行上面的代码片段时,ListIterator的Cursor指向List中的“ Last but one”元素,如上图所示。 执行此过程以将ListIterator的Cursor到达LinkedList的第一个元素。
After reading the first element, if we run the below code snippet, it returns “false” value.
读取第一个元素后,如果我们运行下面的代码片段,它将返回“ false”值。
namesIterator.hasPrevious();
As ListIterator’s Cursor points to the before the first element of the LinkedList, hasPrevious() method returns a false value.
由于ListIterator的Cursor指向LinkedList的第一个元素之前的,hasPrevious()方法返回一个假值。
After observing all these diagrams, we can say that Java ListIterator supports Both Forward Direction and Backward Direction Iterations as shown in the below diagrams. So it is also known as Bi-Directional Cursor.
观察所有这些图之后,我们可以说Java ListIterator同时支持正向和反向迭代,如下图所示。 因此,它也被称为双向光标。
Forward Direction ListIterator
前向ListIterator
Backward Direction ListIterator
向后方向ListIterator
Unlike Iterator, ListIterator has the following advantages:
与Iterator不同,ListIterator具有以下优点:
Compare to Iterator, Java ListIterator has many advantages. However, it still have the following some limitations.
与Iterator相比,Java ListIterator具有许多优点。 但是,它仍然具有以下一些限制。
In this section, we will discuss about Similarities between Java two Cursors: Iterator and ListIterator.
在本节中,我们将讨论Java两个游标:Iterator和ListIterator之间的相似性。
In this section, we sill discuss differences between Java Two Iterators: Iterator and ListIterator.
在本节中,我们将讨论Java两个迭代器之间的区别:迭代器和ListIterator。
Iterator | ListIterator |
---|---|
Introduced in Java 1.2. | Introduced in Java 1.2. |
It is an Iterator for whole Collection API. | It is an Iterator for only List implemented classes. |
It is an Universal Iterator. | It is NOT an Universal Iterator. |
It supports only Forward Direction Iteration. | It supports both Forward and Backward Direction iterations. |
It’s a Uni-Directional Iterator. | It’s a Bi-Directional Iterator. |
It supports only READ and DELETE operations. | It supports all CRUD operations. |
We can get Iterator by using iterator() method. | We can ListIterator object using listIterator() method. |
迭代器 | ListIterator |
---|---|
在Java 1.2中引入。 | 在Java 1.2中引入。 |
它是整个Collection API的迭代器。 | 它是仅用于List实现类的Iterator。 |
它是一个通用迭代器。 | 它不是通用迭代器。 |
它仅支持正向迭代。 | 它支持前进和后退方向迭代。 |
这是一个单向迭代器。 | 这是一个双向迭代器。 |
它仅支持READ和DELETE操作。 | 它支持所有CRUD操作。 |
我们可以使用iterator()方法获取Iterator。 | 我们可以使用listIterator()方法使用ListIterator对象。 |
That’s all of about ListIterator in Java. I hope these Java ListIterator theories and examples will help you in getting started with ListIterator programming.
这就是Java中的ListIterator的全部内容。 我希望这些Java ListIterator理论和示例将帮助您入门ListIterator编程。
Reference: ListIterator API Doc
参考 : ListIterator API文档
翻译自: https://www.journaldev.com/13457/java-listiterator