k.bo
k.bo

Reputation: 15

Iterator and If

I got a little problem with my programm.

Iterator<MyString> it = globalSet.iterator();
Iterator<String> it2 = globalArray.iterator();



        while(it.hasNext() && it2.hasNext())
        {
            System.out.println(it2.next());
            if(it.next().getLine().contains(it2.next()) == true)
            {


                //it.remove();
            }

        }

If i disable the if clause my Output is correct and looks like this:

1;Trägertour 096;1410;Autotour 1N410;
2;Trägertour 097;1410;Autotour 1N410;
3;Trägertour 098;1410;Autotour 1N410;
4;Trägertour 099;1410;Autotour 1N410;
5;Trägertour 100;1410;Autotour 1N410;

But if the if clause is enabled my Output looks like this:

1;Trägertour 096;1410;Autotour 1N410;
3;Trägertour 098;1410;Autotour 1N410;
5;Trägertour 100;1410;Autotour 1N410;

So why?

Upvotes: 0

Views: 81

Answers (1)

Eran
Eran

Reputation: 393851

You advance the same iterator twice in the same iteration of the while loop (by calling it2.next() twice). You shouldn't.

    while(it.hasNext() && it2.hasNext()) {
        String str = it2.next();
        System.out.println(str);
        if(it.next().getLine().contains(str)) {
            //it.remove();
        }
    }

Upvotes: 5

Related Questions