IT Community - Software Programming, Web Development and Technical Support

Difference between enumeration and iterator in java?

This is a discussion on Difference between enumeration and iterator in java? within the Java Programming forums, part of the Software Development category; Difference between enumeration and iterator in java?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Java Programming

Register FAQ Members List Calendar Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 07-30-2007, 12:47 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Question Difference between enumeration and iterator in java?

Difference between enumeration and iterator in java?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-30-2007, 12:56 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Default Re: Difference between enumeration and iterator in java?

Hi...
An Enumeration is used for iterating over a given collection, Usually of unknown size.
Iterator also has the same purpose but Enumeration does not allow modification of the
collection, which can be achieved using Iterator.

Iterator’s “remove” method removes from the underlying collection the last element returned
by the iterator. This method can be called only once per call to the Iterator’s “next” method.

Following code sample will help you understand how to “Enumeration” and “Iterator”.

import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;

public class link {
public static void main (String []str){

Enumeration enum_cities;

Vector cities = new Vector();
cities.add("New York");
cities.add("Sydney");
cities.add("Frankfurt");
cities.add("Delhi");
cities.add("Milan");
cities.add("Paris");

enum_cities = cities.elements();

while (enum_cities.hasMoreElements())
System.out.println(" -Enum- " + enum_cities.nextElement());

Iterator it_cities = cities.iterator();
String str_temp= "";

System.out.println(" Actual size of vector: " + cities.size());

while (it_cities.hasNext())
{
str_temp = it_cities.next().toString();
if(str_temp.equals("Sydney"))
it_cities.remove();
else
System.out.println(" -Iterator- " + str_temp);
}

// iterator removed Sydney from vector so its size is reduced to 5
System.out.println("Current size of vector: " + cities.size());

}

}

If you want to use enumeration with arrays, following code sample code:

import java.lang.reflect.Array;
import java.util.Enumeration;

final public class ArrayFactory {
static public Enumeration makeEnumeration(final Object obj) {
Class type = obj.getClass();
if (!type.isArray()) {
throw new IllegalArgumentException(obj.getClass().toString() );
} else {
return (new Enumeration() {
int size = Array.getLength(obj);

int cursor;

public boolean hasMoreElements() {
return (cursor < size);
}

public Object nextElement() {
return Array.get(obj, cursor++);
}
});
}
}

public static void main(String args[]) {
Enumeration e = makeEnumeration(new int[] { 1, 3, 4, 5 });
while (e.hasMoreElements()) {
System.out.println(e.nextElement());
}

}
}

Cheers
__________________
The OXYGEN
Delivers edgy, intelligent Technology to all...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-08-2007, 03:31 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 194
leoraja8 is on a distinguished road
Default Re: Difference between enumeration and iterator in java?

Iterators differ from enumerations in two ways:
* Iterators allow the caller to remove elements from the underlying collection
during the iteration with well-defined semantics.
* Method names have been improved.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-08-2007, 03:53 AM
krishnakumar krishnakumar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 206
krishnakumar is on a distinguished road
Wink Re: Difference between enumeration and iterator in java?

Hi,

An Enumeration and Iterator provides a one time scan through a list of Objects.

Here i explain how enumerator and iterator is used with vector objects.....

An instance of the Vector class provides a list of any Object() derived types.

It is Not the same as an array. Arrays are of only one type and the number of elements cannot be changed.

Vectors instead can hold a mix of class objects

And they can grow or shrink:

Vector list = new Vector();
list.addElement(" a new string object");
list.addElement(" another new string object");
list.addElement(new Date());
list.addElement(new Date());
list.removeElementAt(3);
...

When a Vector element is returned, it is returned as an Object type. It must be cast to the proper type:

String str = (String)list.firstElement();

If you cast the returned object to a class that it does not belong to, then an runtime error will occur:

String date = (String)list.lastElement();
** Error: the object returned is of the Date type, not a String object. **

You can use instanceof to check first for what kind of object has been returned.

Object o = list.lastElement();
if ( o instanceof String)
String date = (String)o;
else if( o instanceof Date)
Date aDate = (Date)o;


The Vector class has a number of other methods such as a search for the index number

int i = list.indexOf(str);

A Vector can also return an Enumeration.

An Enumeration provides a one time scan through a list of Objects.

Enumeration e = list.elements();
while ( e.hasMoreElements() ) {
System.out.println(e.nextElement().toString());
}

After the hasMoreElements() returns false, the Enumeration cannot be used again.

With Java 1.2 came an alternative to Enumeration called Iterator.

Iterator differs from Enumeration in 2 ways:

* elements can be safely removed from the Vector using the remove(int index)
* the hasMore() and next() methods are more user friendly than nextElement() and hasMoreElements().
__________________
Krishnakumar.S
Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/java-programming/2997-difference-between-enumeration-iterator-java.html
Posted By For Type Date
DiscussWeb IT Community - Fusing This thread Refback 08-08-2007 03:34 AM

Similar Threads
Thread Thread Starter Forum Replies Last Post
An Iterator class vijayanand C and C++ Programming 0 09-21-2007 03:38 AM
difference between an enumeration and a set of preprocessor #defines? prasath C and C++ Programming 1 08-18-2007 12:36 AM
Difference between static and final in java leoraja8 Java Programming 2 08-17-2007 05:49 AM
What is the difference between structures and enumeration? anbuchezhians VB.NET Programming 1 07-28-2007 01:50 AM
Difference between static and final in java leoraja8 Java Programming 2 07-20-2007 06:46 AM


All times are GMT -7. The time now is 11:06 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0