迭代 ArrayList 使用迭代器,其中 while 条件搜索 .contains() 并失败。

huangapple go评论69阅读模式
英文:

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&lt;String&gt; names = new ArrayList&lt;&gt;();
    
for(int i = 0; i &lt; 10; i++) {
   names.add(&quot;Name &quot; + i);
}
    
Iterator&lt;String&gt; it = names.iterator();
while(it.hasNext() &amp;&amp; it.next().contains(&quot;Name 5&quot;)) {
    System.out.println(&quot;Found my desired name.&quot;);
}

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&lt;String&gt; names = new ArrayList&lt;&gt;();

        for (int i = 0; i &lt; 10; i++) {
            names.add(&quot;Name &quot; + i);
        }

        Iterator&lt;String&gt; it = names.iterator();
        while (it.hasNext()) {
            String item = it.next();
            if (item.contains(&quot;Name 5&quot;)) {
                System.out.println(&quot;Found my desired name.&quot;);
                break;//remove if you don&#39;t want to exit
            }
        }

The problem with your code is the while loop.

while(it.hasNext() &amp;&amp; it.next().contains(&quot;Name 5&quot;)) {

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&lt;String&gt; it = names.iterator();
while(it.hasNext()) {
    if (it.next().contains(&quot;Name 5&quot;)) {
        System.out.println(&quot;Found my desired name.&quot;);
        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 &amp;&amp; false - will stop the looping

huangapple
  • 本文由 发表于 2020年10月9日 00:47:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64267114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定