Java Using iterator for arraylist how do you get index -


if i'm using iterator how index in condition met? iterator.next() gives me 1 after can minus 1 figure right way this?

public static int getsomething()     int tempposition = 1;             iterator<item> iterator = data.iterator();             while (iterator.hasnext())             {                 if (iterator.next().gettitle().equals("something"))                 {                     tempposition = iterator.next().getposition();                 }               }  return tempposition - 1; 

what found correct code situation.

public static int getsomething()     {         int tempposition = 1;          iterator<item> iterator = data.iterator();          while (iterator.hasnext())         {           item item = iterator.next();                if (item.gettitle().equals("something"))             {                 tempposition = item.getposition();             }          }           log.d(tag, "tempposition " + tempposition );         return tempposition ;       } 

you unexpected value tempposition because call next twice in loop. should instead:

item item = iterator.next(); // call next once here if (item.gettitle().equals("something")) {     tempposition = item.getposition(); } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -