fredag 1 juni 2012

Iterator

Lecture 19.

Iterator
  -> lists through set of values

ArrayList

ArrayList<String> name new ArrayList<String>( );
Iterator<String> it = names.iterator( );

//Always ask if there are any values left
   while(it.hasNext()){
       println(it.next());
 }

HashMap

Don't have the values in order.

//You ask for the key and not the corresponding value
   Iterator<String> i = phonebook.keySet( ).iterator( );
//And then use the whileloop

If you don't want to create the iterator there is a new way of writing:
   for (String name : phonebook.keySet()){
          println(name)
}