KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > util > Iterator

java.util
Interface Iterator<E>

All Known Subinterfaces:
ListIterator<E>
All Known Implementing Classes:
BeanContextSupport.BCSIterator, Scanner
See Also:
Top Examples, Source Code, Collection, Enumeration

boolean hasNext()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[155]Iterator looping
By Anonymous on 2005/06/20 14:30:45  Rate
Foo foo; 
 for  (  Iterator iter = foos.iterator (  ) ;  
       iter.hasNext (  ) ;  
       foo = iter.next (  )   )   
  {  
      // use foo 
  }  
 


[156]_
By Anonymous on 2005/06/20 14:30:59  Rate
iter = foos.iterator (  ) ; 
 while (  iter.hasNext (  )   ) ; 
  {  
     Foo foo =  ( Foo )  iter.next (  ) ; 
  }  
 


[615]_
By Anonymous on 2004/06/04 20:58:16  Rate
// Correction to the above - remove the semicolon at end of 'while' line 
 iter = foos.iterator (  ) ;  
 while (  iter.hasNext (  )   )   {   
     Foo foo =  ( Foo )  iter.next (  ) ;  
  }   
 


[1110]_
By Anonymous on 2004/11/09 14:11:04  Rate
the for loop above will not work because "foo" is never initialized the very first time through.

[1454]_
By JavaDude on 2005/10/07 08:41:26  Rate
// No need for the excess variable scope. Always keep  
 // variable scope within the loop. 
  
  
 for (  Iterator iter = coll.iterator (  ) ; iter.hasNext (  ) ;  )   
  {  
     Foo foo =  ( Foo ) iter.next (  ) ; 
      
     // do stuff 
  }  
  
  
 


[1944]how to use Iterator and hasNext correctly
By java_lang { at } yahoo { dot } co { dot } uk on 2007/11/24 18:01:20  Rate
I am new to learn Java. I got the message "Iterator is a raw type. References togeneric type Iterator < E >  should be parameterized" 
  
  
 public class Bank  {  
   public int getTotalAssets (  )   {  
     int totalAssets = 0; 
     Iterator iter = theAccounts.iterator (  ) ; 
     while ( iter.hasNext (  )  == true )   {  
       Account acc =  ( Account ) iter.next (  ) ; 
       totalAssets += acc.getBalance (  ) ; 
      }  
     return totalAssets; 
  }  
  
  
 And I called the above method in another class, also got error "The method getTotalAsset (  )  is undefined for the type Bank" 
  
  
 public class Application  {  
   public void run (  )  {  
           int totalAssets = bk.getTotalAsset (  ) ; //Use-case: Obtain total assets 
     ConsoleIO.out.println ( "Total assests: " + totalAssets ) ; 
          }  
  }  
  
  
 annie 
 


E next()
See Also:
NoSuchElementException, hasNext()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void remove()
See Also:
IllegalStateException, UnsupportedOperationException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1239]Iterator Example
By Srinivasa Rao Meenavalli on 2005/07/07 09:01:16  Rate
import java.util.Iterator; 
 import java.util.HashSet; 
 public class IteratorExample 
  {  
   public static void main ( String [  ]  args )   
    {  
     HashSet set=new HashSet (  ) ; 
     Object obj1= ( Object ) new String ( "String1" ) ; 
     Object obj2= ( Object ) new String ( "String2" ) ; 
     Object obj3= ( Object ) new String ( "String3" ) ; 
     set.add ( obj1 ) ; 
     set.add ( obj2 ) ; 
     set.add ( obj3 ) ; 
     Iterator iterator=set.iterator (  ) ; 
     while ( iterator.hasNext (  )  )  {  
       String str= ( String ) iterator.next (  ) ; 
       System.out.println ( str ) ; 
       iterator.remove (  ) ; 
      }  
     while ( iterator.hasNext (  )  )  {  
       System.out.println ( "Next="+iterator.next (  )  ) ; 
      }  
      
  
  
    }  
  } 

Popular Tags