dcsimg
Is there any advantage of using iterator rather than index simply for arraylist traversal
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   gomathy_anusuyaraj
Posted On:   Wednesday, April 6, 2005 01:42 AM

Is there any advantage of using iterator rather than index simply for arraylist traversal To be more specific , an arraylist can be traversed either using an iterator or using index thru for loop , as below : ArrayList theArrayList = new ArrayList() ; for(int i=0 ;i <150000;i++) { theArrayList .add(new Integer(i)); } // Traversal using Iterator for(Iterator iter = theArrayList .iterator(); iter.hasNext();) { Integer activity1 = (Integer) iter.next(); System.out.println("activityl " + activity1); } ( or ) // Traversal using Index for (int i = 0; i < theArray   More>>


Is there any advantage of using iterator rather than index simply for arraylist traversal


			
To be more specific , an arraylist can be traversed either using an
iterator or using index thru for loop , as below :

ArrayList theArrayList = new ArrayList() ;
for(int i=0 ;i <150000;i++)
{
theArrayList .add(new Integer(i));
}


// Traversal using Iterator
for(Iterator iter = theArrayList .iterator(); iter.hasNext();)
{
Integer activity1 = (Integer) iter.next();
System.out.println("activityl " + activity1);
}

( or )



// Traversal using Index

for (int i = 0; i < theArrayList .size(); i++)
{
Integer activity1 = (Integer) theArrayList .get(i);
System.out.println("activityl " + activity1);
}




What is the advantage of using an iterator rather than an index just for traversal -
in terms of performance / memory allocation /any other factor ?

   <<Less

Re: Is there any advantage of using iterator rather than index simply for arraylist traversal

Posted By:   Ravi_Kesariraman  
Posted On:   Wednesday, April 6, 2005 02:36 AM

I hope this article may answer your question:

http://www.onjava.com/pub/a/onjava/2001/10/23/optimization.html

About | Sitemap | Contact