Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Enumeration/Iterator

  Asked By: John    Date: Aug 21    Category: Java    Views: 667
  

I have a little wondering about Iterator and Enumeration.
What is the benefit using one of these interface ? from all my
understanding, it just used for looping. But there's Vector or Hashcode or
any class that support looping elements.
I saw lots code that using those interface only for looping. I saw a
Collection object changed into Iterator and in the next line i found that it
just for looping and get the Collection value, or in the sun example about
Enumaration, it changed Vector into Enumeration and do Enumeration.next() to
looping and get the value.
I can do that with For () Statement, without using Iterator or Enumeration.
Anyone can gimme good explanation about the reason ?

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Norman Ray     Answered On: Aug 21

I see a couple of reasons for using Iterators instead of for loops:

1.) Iterators handle most looping  cases more cleanly than for loops
2.) Iterator interface  can hide potentially complicated iteration logic

 
Didn't find what you were looking for? Find more on Enumeration/Iterator Or get search suggestion and latest updates.