英文:
Iterating over ArrayList with Iterator where while condition searches for .contains() and fails
问题
这不应该是什么高深的问题,但出于某种原因,我的 while 条件始终为 false。我创建了一个空的 ArrayList
并填充了它。然而,当我遍历它时,似乎出现了问题。我错过了什么吗?
ArrayList<String> names = new ArrayList<>();
for(int i = 0; i < 10; i++) {
names.add("Name " + i);
}
Iterator<String> it = names.iterator();
while(it.hasNext() && it.next().contains("Name 5")) {
System.out.println("Found my desired name.");
}
在 Netbeans 中执行了上述代码。
英文:
This shouldn't be rocket sience but for some reason my while-condition stays false. I create an empty ArrayList
which I fill. However when I iterate over it, it seems to fail. What am I missing?
ArrayList<String> names = new ArrayList<>();
for(int i = 0; i < 10; i++) {
names.add("Name " + i);
}
Iterator<String> it = names.iterator();
while(it.hasNext() && it.next().contains("Name 5")) {
System.out.println("Found my desired name.");
}
Executed the appended code as-is in Netbeans.
答案1
得分: 1
以下是已经翻译好的内容:
正确的代码片段解决您的问题应为:
List<String> names = new ArrayList<>();
for (int i = 0; i < 10; i++) {
names.add("Name " + i);
}
Iterator<String> it = names.iterator();
while (it.hasNext()) {
String item = it.next();
if (item.contains("Name 5")) {
System.out.println("找到了我需要的名字。");
break; // 如果不想退出,可以移除这一行
}
}
您的代码问题在于 while 循环部分:
while (it.hasNext() && it.next().contains("Name 5")) {
这基本上意味着在迭代器还有值并且下一个值(在这种情况下将是第一个元素)包含 "Name 5" 时,保持运行。问题在于,第一个项将是 "Name 0" 而不是 "Name 5"。"Name 0" 不包含 "Name 5"。
因此,循环运行的条件将为假,循环将不会执行。
英文:
The correct code snippet for your problem would be
List<String> names = new ArrayList<>();
for (int i = 0; i < 10; i++) {
names.add("Name " + i);
}
Iterator<String> it = names.iterator();
while (it.hasNext()) {
String item = it.next();
if (item.contains("Name 5")) {
System.out.println("Found my desired name.");
break;//remove if you don't want to exit
}
}
The problem with your code is the while loop.
while(it.hasNext() && it.next().contains("Name 5")) {
This basically means while the iterator has a value and the next value(which will be the first element in this case) contains Name 5, keep running. The problem with this is, that the first item will be "Name 0" not "Name 5". "Name 0" does not contain Name 5.
So what happens is that the condition for the while loop to run will be false and the loop won't run.
答案2
得分: 0
对于第一次迭代,it.next()
返回列表的第一个元素,即 Name 0
,因此它永远不会进入 while
循环。
你应该按照以下方式修复代码:
Iterator<String> it = names.iterator();
while (it.hasNext()) {
if (it.next().contains("Name 5")) {
System.out.println("找到了我想要的名字。");
break; // 退出 while 循环
}
}
英文:
For the first iteration, it.next()
returns the first element of the list, which is Name 0
, so it never enters the while
loop.
You should fix the code this way:
Iterator<String> it = names.iterator();
while(it.hasNext()) {
if (it.next().contains("Name 5")) {
System.out.println("Found my desired name.");
break; // exit while loop
}
}
答案3
得分: 0
while
循环将持续循环,直到条件不为真。
在你的示例中,条件是true && false
- 这将停止循环。
英文:
while
cycle will loop until your condition is not true.
In your example it is true && false
- will stop the looping
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论