Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Iterator in Java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 99
    Comment on it

    In collection, there would be many situations where we need to retrieve the collection elements. Example, we need to show each element. For these situations Iterator is the best solution.


    How it implements :-

  • First iterator comes to start point of the collection by using iterator().
  • Call hasNext() until it returns false.
  • Get each element in the loop using next().

      ArrayList mArrayList = new ArrayList(); 
     // adding elements to ArrayList object 
      mArrayList.add("A"); 
      mArrayList.add("N"); 
      mArrayList.add("D"); 
      mArrayList.add("R"); 
      mArrayList.add("O"); 
      mArrayList.add("I"); 
      mArrayList.add("D"); 
    
    
      System.out.print("ArrayList :  "); 
      Iterator iterator = al.iterator(); 
      while(iterator.hasNext()) { 
         Object obj = iterator.next(); 
         System.out.println(obj+ " "); 
      } 
    

    Output :-
    ArrayList : A N D R O I D

Comment on it

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: