- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
具有两个元素的ArrayList
ArrayList<String> a = new ArrayList<String>() {
{
add("hai");
add("hello");
}
};
为该列表创建了一个列表迭代器
ListIterator l = a.listIterator();
使用 next 遍历并向列表添加元素“bye”
while (l.hasNext()) {
l.next();
l.add("bye");
}
System.out.println(a);
以上代码的输出为= [hai,bye,hello,bye]
遍历到列表的最后一个
while(l.hasNext()) {
l.next();
}
使用 prev 向前遍历并将元素“bye”添加到列表
while (l.hasPrevious()) {
l.previous();
l.add("bye");
}
System.out.println(a);
无限且不打印任何内容!!!
最佳答案
嗯,ListIterator
的 add
的 Javadoc 解释说:
Inserts the specified element into the list (optional operation). The element is inserted immediately before the element that would be returned by next, if any, and after the element that would be returned by previous, if any. (If the list contains no elements, the new element becomes the sole element on the list.) The new element is inserted before the implicit cursor: a subsequent call to next would be unaffected, and a subsequent call to previous would return the new element.
l.previous
返回您刚刚通过 l.add("bye")
添加的元素,因此您永远不会到达 的开头列表
。因此无限循环。
如果列表
如下所示:
hai hello
^
|
cursor
调用l.previous();
返回“hello”并将光标向后移动:
hai hello
^
|
cursor
调用 l.add("bye");
在“hai”和“hello”之间以及光标之前添加新元素:
hai bye hello
^
|
cursor
因此下一次调用l.previous()
将返回“bye”。
因此你的循环永远不会到达List
的开头。
关于java - 为什么listiterator中的hasPrevious()和add()组合会进入无限循环,而hasNext()和add()则不会?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59711892/
有人可以告诉我,如果 (ListIterator it = listIterator(); it.hasNext(); )部分代码应写为 for (ListIterator it = list.lis
我在替换数组列表中的项目时遇到问题。我搜索了 soultion,发现它是由 listIterator 完成的。所以我想实现它,但不知何故迭代器的“set”方法不可用。为什么?整个事情都发生在我的 sp
我有两个 ArrayList,每个都包含一定大小的块:blockList、eraseList。块是具有两个字段的对象:开始和结束。我需要从另一组块中减去一组块。 我必须遍历删除器列表并从它们重叠的块列
我有一个自定义的、通用的、单独的 LinkedList,它是我自己构建的。我可以在列表中添加、删除等。我想在我的类中实现 Java ListIterator。我将如何开始这个?我需要将哪些方法添加到我
我正在努力实现一种检查 ArrayList 中连续相等元素的最大数量的方法: public class ArrayReader { public int getMaxConsecutiveEq
我有一个简单的 Java LinkedList,它有 10 个元素。在这种状态下,我得到一个普通的 ListIterator,比如 .listIterator(3)。如果我随后将其他元素插入/删除到列
我正在读一篇thread这里介绍一下java ArrayList和LinkedList的性能。有来自Mr Kevin Brock的答复内容如下。 "Linked list add is not alw
我有一个引用类型的数组。我想使用 hasPrevious() 和 hasNext() 方法。我想使用ListIterator。将数组转换为 ListIterator 的正确方法吗? 最佳答案 我发现执
我有一个简短的问题,关于 ListIterators(我想,通常是 Iterators)在 Java 中的表现。例如,对于一个链表(Java 的标准链表),如果我为其获取一个 ListIterator
我需要在ListIterator“nodeListIterator”中交换node和node.getNext(),并且我已经尝试了几个小时 if (node instanceof LdcInsnNo
所以我的问题是,是否可以通过使用带有列表名称(foodList、foodAmount 等)的数组来简化这段代码,而不是用列表名称(来自所述数组)进行某种循环来替换迭代器中的占位符? 这是代码: pub
package wrap; import java.util.*; public class ArrayListDemo { public static void main(String []
我目前正在尝试学习如何实现我自己的 ListIterators。除了我对 previous() 方法感到困惑外,我已经实现了其中的大部分并准备就绪。按照标准惯例,我可以解释一下 previous()
我有如下所示的代码片段: ArrayList a = new ArrayList(); ListIterator p = a.listIterator(); 但是,我注意到您实际上不需要为 Li
代码: Random Picker = new Random(); List list = new ArrayList(); list.add("card1"); list.add("ca
关于Java中迭代的问题。我(在某种程度上)熟悉 Iterator、ListIterator 和 Iterable 接口(interface),即我了解它们背后的想法。但这也是我的问题所在。 如果
我有两个类:ParentClass 和 SubClass。子类继承自父类。我有以下代码:(在类内) List lstSub; //some initialization public ListIter
我编写了以下使用列表迭代器函数的代码,为什么没有打印出结果,谁能告诉我出了什么问题?谢谢。 import java.util.ArrayList; import java.util.ListItera
如何从 ListIterator 中删除所有元素,然后添加新元素。我在最后一行添加到迭代器时遇到 ConcurrentModificationException 。 public static voi
我正在使用 Java 进行编程,我创建了一个类来获取一个迭代器,因为我在此代码中仅使用一个列表 public ListIterator iterateur() { ListIterator i
我是一名优秀的程序员,十分优秀!