Iterator vs Array
1 posts in topic
Flat View  Flat View

Posted By:   Dileep_Nair
Posted On:   Thursday, February 7, 2002 11:41 AM

When do we use array and when do we use iterator, as the return type of a function, when we know all the values to be returned?

Is there any benefit in using the iterator as the return type when we have to return a finite number of objects and all those objects are known within the function? Isn't it advantageous to use the array instead?

Re: Iterator vs Array

Posted By:   Anonymous  
Posted On:   Thursday, February 7, 2002 01:22 PM

An array is advantageous for two reasons. First, an iterator always returns an object, and you have to cast the right class to get what you wanted, which takes some cpu time. Also, when getting a object, you have call a method, which creates a lot of overhead in a loop. An iterator can be useful when you have a list of variable length, or you want to prevent some other class from accessing directly some method for a collection, for example add().
About | Sitemap | Contact