Java iterator stuff -


public class show {     public static arraylist ara = new arraylist();     public static iterator snake;      public static void kai(){         ara.add(1);         ara.add(2);         ara.add(5);         ara.add(7);         ara.add(10);         ara.add(13);                 snake = ara.iterator();         while(snake.hasnext()){             system.out.println(snake.next());             if(snake.next()==7)break;         }     }             public static void main(string[] args){         kai();     } } 

at execution, 1, 5, 10 consecutively prints out. how explain this? expected 1, 2, 5 print out instead.

you should change code following:

public static void kai(){     ara.add(1);     ara.add(2);     ara.add(5);     ara.add(7);     ara.add(10);     ara.add(13);             snake = ara.iterator();     while(snake.hasnext()){         int value = (int) snake.next();         system.out.println(value );         if(value ==7)break;     } }     

that way call iterator.next() 1 time inside while loop.


Comments

Popular posts from this blog

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

arrays - Algorithm to find ideal starting spot in a circle -

php - Autoloader issue not returning Class -