英文:
Why do I get noSuchElementException when using listIterator.add()?
问题
没有进一步的延迟,我将立即发布我的代码。它非常简单,所以你不应该在理解它方面遇到任何问题。我的想法是在找到“blue”后,使用listIterator
在其后面“添加”颜色。显然这里有些问题。
public class Main {
public static void main(String[] args) {
Color c1 = new Color("red");
Color c2 = new Color("blue");
Color c3 = new Color("green");
List<Color> list = new ArrayList<>();
list.add(c1);
list.add(c2);
list.add(c3);
ListIterator<Color> iterator = list.listIterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
if (iterator.next().tone.equals("blue")){
iterator.add(new Color("yellow"));
}
}
}
}
class Color {
String tone;
public Color(String tone) {
this.tone = tone;
}
@Override
public String toString() {
return tone;
}
}
输出:
red
green
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.ArrayList$Itr.next(ArrayList.java:999)
at Main.main(Main.java:21)
英文:
Without further delay, I will post my code straight away. It is very simple so you shouldn't find any problems understanding it. My idea is to add
color after it found "blue" using listIterator
. Apparently there is something wrong here.
public class Main {
public static void main(String[] args) {
Color c1 = new Color("red");
Color c2 = new Color("blue");
Color c3 = new Color("green");
List<Color> list = new ArrayList<>();
list.add(c1);
list.add(c2);
list.add(c3);
ListIterator<Color> iterator = list.listIterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
if (iterator.next().tone.equals("blue")){
iterator.add(new Color("yellow"));
}
}
}
}
class Color {
String tone;
public Color(String tone) {
this.tone = tone;
}
@Override
public String toString() {
return tone;
}
}
OUTPUT:
red
green
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.ArrayList$Itr.next(ArrayList.java:999)
at Main.main(Main.java:21)
答案1
得分: 2
你调用hasNext()
一次,可以调用next()
两次。调用一次并保存,以便能够多次使用。
while (iterator.hasNext()){
Color current = iterator.next();
System.out.println(current);
if (current.tone.equals("blue")){
iterator.add(new Color("yellow"));
}
}
英文:
You call hasNext()
once for 2 calls of next()
. Call it once and save it to be able to use multiple times
while (iterator.hasNext()){
Color current = iterator.next();
System.out.println(current);
if (current.tone.equals("blue")){
iterator.add(new Color("yellow"));
}
}
答案2
得分: 1
我知道答案已经提供了,但是如果有帮助的话就发出来:
请参考以下代码:-
public static void main(String[] args) {
Color c1 = new Color("red");
Color c2 = new Color("blue");
Color c3 = new Color("green");
List<Color> list = new ArrayList<>();
list.add(c1);
list.add(c2);
list.add(c3);
ListIterator<Color> iterator = list.listIterator();
System.out.println(iterator.next());//-->red
System.out.println(iterator.next());//-->blue
System.out.println(iterator.next());//-->green
System.out.println(iterator.next());//-->java.util.NoSuchElementException (iterator is moved forward but list is exhausted)
}
所以基本上 `iterator.next()` 会定位到可迭代集合中的元素。
因此在您的 `while` 循环中多次调用 `iterator.next()` 会每次将光标移动到下一个值,因此最好将 `iterator.next()` 存储在循环内的变量中。
英文:
I know the answer is provided but posting if it helps
refer below code:-
public static void main(String[] args) {
Color c1 = new Color("red");
Color c2 = new Color("blue");
Color c3 = new Color("green");
List<Color> list = new ArrayList<>();
list.add(c1);
list.add(c2);
list.add(c3);
ListIterator<Color> iterator = list.listIterator();
System.out.println(iterator.next());//-->red
System.out.println(iterator.next());//-->blue
System.out.println(iterator.next());//-->green
System.out.println(iterator.next());//-->java.util.NoSuchElementException (iterator is moved forward but list is exhausted)
}
so basically iterator.next()
locates the element in the iterable collection.
hence calling iterator.next()
multiple time in ur while
loop will shift the cursor eveytime to next value , hence its better to store iterator.next()
into a variable inside the loop.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论