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

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -