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 。
Java 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实现的对象中逐个迭代元素。
- It is available since Java 1.2. 从Java 1.2开始可用。
- It extends Iterator interface. 它扩展了Iterator接口。
- It is useful only for List implemented classes. 它仅对List实现的类有用。
- Unlike Iterator, It supports all four operations: CRUD (CREATE, READ, UPDATE and DELETE). 与Iterator不同,它支持所有四个操作:CRUD(CREATE,READ,UPDATE和DELETE)。
- Unlike Iterator, It supports both Forward Direction and Backward Direction iterations. 与Iterator不同,它同时支持正向和反向迭代。
- It is a Bi-directional Iterator. 它是一个双向迭代器。
- It has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next(). 它没有当前元素; 它的光标位置始终位于通过调用previous()返回的元素和通过调用next()返回的元素之间。
NOTE:- What is CRUD operations in Collection API?
注意:-什么是Collection API中的CRUD操作?
- CREATE: Adding new elements to Collection object. 创建:向Collection对象添加新元素。
- READ: Retrieving elements from Collection object. 阅读:从Collection对象中检索元素。
- UPDATE: Updating or setting existing elements in Collection object. 更新:更新或设置Collection对象中的现有元素。
- DELETE: Removing elements from Collection object. 删除:从集合对象中删除元素。
Java ListIterator类图 (Java ListIterator Class Diagram)
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方法 (Java ListIterator Methods)
Java ListIterator interface has the following Methods.
Java ListIterator接口具有以下方法。
- void add(E e): Inserts the specified element into the list. void add(E e):将指定的元素插入列表。
- boolean hasNext(): Returns true if this list iterator has more elements when traversing the list in the forward direction. boolean hasNext():如果在向前遍历列表时此列表迭代器包含更多元素,则返回true。
- boolean hasPrevious(): Returns true if this list iterator has more elements when traversing the list in the reverse direction. boolean hasPrevious():如果在反向遍历列表时此列表迭代器包含更多元素,则返回true。
- E next(): Returns the next element in the list and advances the cursor position. E next():返回列表中的下一个元素并前进光标位置。
- int nextIndex(): Returns the index of the element that would be returned by a subsequent call to next(). int nextIndex():返回元素的索引,该元素的索引将由对next()的后续调用返回。
- E previous(): Returns the previous element in the list and moves the cursor position backwards. E previous():返回列表中的上一个元素,并将光标位置向后移动。
- int previousIndex(): Returns the index of the element that would be returned by a subsequent call to previous(). int previousIndex():返回元素的索引,该元素的索引将由对next()的后续调用返回。
- void remove(): Removes from the list the last element that was returned by next() or previous(). void remove():从列表中移除next()或previous()返回的最后一个元素。
- void set(E e): Replaces the last element returned by next() or previous() with the specified element. void set(E e):将next()或previous()返回的最后一个元素替换为指定的元素。
We will explore these methods one-by-one with useful examples in the coming sections.
我们将在接下来的部分中逐一探讨这些方法,并提供有用的示例。
Java ListIterator基本示例 (Java ListIterator Basic Example)
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<E> 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<String> names = new LinkedList<>();
names.add("Rams");
names.add("Posa");
names.add("Chinni");
// Getting ListIterator
ListIterator<String> namesIterator = names.listIterator();
// Traversing elements
while(namesIterator.hasNext()){
System.out.println(namesIterator.next());
}
// Enhanced for loop creates Internal Iterator here.
for(String name: names){
System.out.println(name);
}
}
}
Output:-
输出:-
Rams
Posa
Chinni
ListIterator双向迭代示例 (ListIterator Bi-Directional Iteration Example)
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<String> names = new LinkedListt<>();
names.add("Rams");
names.add("Posa");
names.add("Chinni");
// Getting ListIterator
ListIterator<String> listIterator = names.listIterator();
// Traversing elements
System.out.println("Forward Direction Iteration:");
while(listIterator.hasNext()){
System.out.println(listIterator.next());
}
// Traversing elements, the iterator is at the end at this point
System.out.println("Backward Direction Iteration:");
while(listIterator.hasPrevious()){
System.out.println(listIterator.previous());
}
}
}
Output:-
输出:-
Forward Direction Iteration:
Rams
Posa
Chinni
Backward Direction Iteration:
Chinni
Posa
Rams
Java迭代器的类型 (Types of Java Iterators)
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。 我们可以将它们分为两种主要类型,如下所示:
- Uni-Directional Iterators 单向迭代器
They are Cursors which supports only Forward Direction iterations. For instance, Enumeration, Iterator, etc. are Uni-Directional Iterators.
它们是仅支持正向迭代的游标。 例如,Enumeration,Iterator等是单向迭代器。
- Bi-Directional Iterators 双向迭代器
They are Cursors which supports Both Forward Direction and Backward Direction iterations. For instance, ListIterator is Bi-Directional Iterator.
它们是同时支持向前和向后迭代的游标。 例如,ListIterator是双向迭代器。
Java ListIterator如何在内部工作? (How Java ListIterator Works Internally?)
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可以在两个方向上工作,这意味着它既可以在正向也可以在反向进行。 它是一个双向迭代器。 为了支持此功能,它具有两组方法。
- Forward Direction Iteration methods 正向迭代方法
We need to use the following methods to support Forward Direction Iteration:
我们需要使用以下方法来支持正向迭代:
- hasNext()) hasNext())
- next() 下一个()
- nextIndex() nextIndex()
- Backward Direction Iteration methods 后向迭代方法
We need to use the following methods to support Backward Direction Iteration:
我们需要使用以下方法来支持向后方向迭代:
- hasPrevious() hasPrevious()
- previous() 以前()
- previousIndex() previousIndex()
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<String> 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<String> 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
ListIterator的优点 (Advantages of ListIterator)
Unlike Iterator, ListIterator has the following advantages:
与Iterator不同,ListIterator具有以下优点:
- Like Iterator, it supports READ and DELETE operations. 与Iterator一样,它支持READ和DELETE操作。
- It supports CREATE and UPDATE operations too. 它也支持CREATE和UPDATE操作。
- That means, it supports CRUD operations: CREATE, READ, UPDATE and DELETE operations. 这意味着它支持CRUD操作:CREATE,READ,UPDATE和DELETE操作。
- It supports both Forward direction and Backward direction iteration. That means it’s a Bi-Directional Java Cursor. 它支持正向和反向迭代。 这意味着它是双向Java游标。
- Method names are simple and easy to use them. 方法名称简单易用。
ListIterator的局限性 (Limitations of ListIterator)
Compare to Iterator, Java ListIterator has many advantages. However, it still have the following some limitations.
与Iterator相比,Java ListIterator具有许多优点。 但是,它仍然具有以下一些限制。
- It is an Iterator only List implementation classes. 它是仅Iterator的List实现类。
- Unlike Iterator, it is not applicable for whole Collection API. 与Iterator不同,它不适用于整个Collection API。
- It is not a Universal Java Cursor. 它不是通用Java游标。
- Compare to Spliterator, it does NOT support Parallel iteration of elements. 与Spliterator相比,它不支持元素的并行迭代。
- Compare to Spliterator, it does NOT support better performance to iterate large volume of data. 与Spliterator相比,它不支持更好的性能来迭代大量数据。
Iterator和ListIterator之间的相似之处 (Similarities between Iterator and ListIterator)
In this section, we will discuss about Similarities between Java two Cursors: Iterator and ListIterator.
在本节中,我们将讨论Java两个游标:Iterator和ListIterator之间的相似性。
- Bother are introduced in Java 1.2. Java 1.2中引入了麻烦。
- Both are Iterators used to iterate Collection or List elements. 两者都是用于迭代Collection或List元素的迭代器。
- Both supports READ and DELETE operations. 两者都支持READ和DELETE操作。
- Both supports Forward Direction iteration. 两者都支持正向迭代。
- Both are not Legacy interfaces. 两者都不是旧版接口。
Iterator和ListIterator之间的区别 (Differences between Iterator and 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文档