1. Overview
1.概述
When we work with Java, efficiently navigating through collections is a common requirement. When dealing with lists, the ListIterator interface provides a powerful tool for bidirectional traversal. However, there are situations where resetting the ListIterator to the first element becomes necessary.
当我们使用 Java 时,高效地浏览集合是一个常见的要求。在处理列表时,ListIterator 接口为双向遍历提供了强大的工具。不过,在某些情况下,有必要将 ListIterator 重置为第一个元素。
In this tutorial, we will explore the various ways to reset a ListIterator to the beginning of a list in Java.
在本教程中,我们将探讨将 ListIterator 重置为 Java 中列表开头的各种方法。
2. Introduction to the Problem
2.问题介绍
As usual, let’s understand the problem through an example.
像往常一样,让我们通过一个例子来理解这个问题。
Let’s say we have a list of strings:
假设我们有一个字符串列表:
List<String> MY_LIST = List.of("A", "B", "C", "D", "E", "F", "G");
Then, we can obtain a ListIterator on MY_LIST by MY_LIST.listIterator() and iterate through the list by calling ListIterator‘s next() method.
然后,我们可以通过 MY_LIST.listIterator() 获得 MY_LIST 上的 ListIterator 并通过调用 ListIterator 的 next() 方法遍历整个列表。
Sometimes, we may want to reset the ListIterator object by asking it to point to the position right before the first element in the list again, as it was newly created.
有时,我们可能希望重置 ListIterator 对象,方法是 要求它再次指向列表中第一个元素之前的位置,因为它是新创建的。
Next, we’ll look at different approaches to solving this problem. Also, we’ll leverage unit test assertions to verify whether each solution gives the expected result.
接下来,我们将研究解决这一问题的不同方法。此外,我们还将利用单元测试断言来验证每种解决方案是否都能得到预期的结果。
3. Creating a New ListIterator
3.创建新的 ListIterator
We know that when we create a new ListIterator object, it points to the beginning of the target list. Therefore, the easiest way to reset a ListIterator instance would be to reassign the variable to a new ListIterator.
我们知道,当我们创建一个新的 ListIterator 对象时,它将指向目标列表的开头。因此,重置 ListIterator 实例的最简单方法就是将变量重新分配给新的 ListIterator 对象。
Let’s create a test and verify if this idea works as expected:
让我们创建一个测试,验证这个想法是否如预期般有效:
ListIterator<String> lit = MY_LIST.listIterator();
lit.next();
lit.next();
lit.next();
lit.next();
lit = MY_LIST.listIterator();
assertFalse(lit.hasPrevious());
assertEquals("A", lit.next());
As the test above shows, after we created a ListIterator lit, we called the lit.next() method four times. When we would like to reset lit, we created a new ListIterator instance and reassigned it to lit.
如上面的测试所示,在我们创建了 ListIterator lit 之后,我们调用了 lit.next() 方法四次。当我们想重置 lit 时,我们创建了一个新的 ListIterator 实例,并将其重新分配给 lit. 。
Then, we verify if lit is reset successfully by two assertions:
然后,我们通过两个断言来验证 lit 是否重置成功:
- lit.hasPrevious() returns false
- lit.next() should be the first element in MY_LIST (“A”)
If we give this test a run, it passes. Therefore, creating a new ListIterator solves our problem.
如果我们运行这个测试,它就会通过。因此,创建一个新的 ListIterator 就能解决我们的问题。
4. Iterating Backward to the Beginning of the List
4.向后迭代到列表开头
Creating a new ListIterator can quickly navigate to the beginning of a list. However, we’ll have a new ListIterator object.
创建一个新的 ListIterator 可以快速导航到列表的开头。不过,我们将有一个新的 ListIterator 对象。
Sometimes, we want to keep the original ListIterator object and move its pointer back to the beginning of the target list. If this is the case, we can utilize ListIterator‘s bidirectional iteration feature to iterate backward to the beginning of the list.
有时,我们希望保留原始 ListIterator 对象,并将其指针移回目标列表的开头。如果是这种情况,我们可以利用 ListIterator 的双向迭代功能,向后迭代到列表的开头。
Next, let’s create a test to see how to achieve that:
接下来,让我们创建一个测试,看看如何实现这一目标:
ListIterator<String> lit = MY_LIST.listIterator();
lit.next();
lit.next();
lit.next();
lit.next();
while (lit.hasPrevious()) {
lit.previous();
}
assertFalse(lit.hasPrevious());
assertEquals("A", lit.next());
As we can see, we apply the backward iteration through a while loop.
正如我们所见,我们通过 while 循环进行后向迭代。
If we run the test, it passes. So, it does the job.
如果我们运行测试,它就会通过。因此,它完成了任务。
It’s worth noting since this approach iterates from the current position backward to the beginning of the list, it can be slow if the number of elements in the list is significant.
值得注意的是,由于这种方法是从当前位置向后迭代到列表的起始位置,因此如果列表中的元素数量很多,迭代速度可能会很慢。
5. Conclusion
5.结论
In this article, we’ve explored two approaches to resetting a ListIterator to the beginning of the list.
在本文中,我们探讨了将 ListIterator 重置为列表开头的两种方法。
If it’s required to keep the original ListIterator instance, we can iterate backward to the head of the list. Otherwise, creating a new ListIterator would be the most straightforward solution.
如果需要保留原始 ListIterator 实例,我们可以向后遍历到列表的头部。否则,创建一个新的 ListIterator 将是最直接的解决方案。
As always, the complete source code for the examples is available over on GitHub.
与往常一样,这些示例的完整源代码可在 GitHub 上获取。