This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Java in General and the fly likes Use Enumeration/Iterator OR array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Use Enumeration/Iterator OR array?" Watch "Use Enumeration/Iterator OR array?" New topic

Use Enumeration/Iterator OR array?

Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi all,
i've a question. what would be pros/cons of using an Enumerator/Iterator based traversin vs. array.
i've this question as i've a scenario where i have multiple objects of the same type needs to be returned. should i use array? or should i use a structure that can be accessed via enumerator/iterator?
i've 2 points in mind regarding this. please vote more points in +ve/-ve side for any.
1. array: might run into array index bounds problems
2. array: will be easy to access via indexes
i don't see any major significant usage issues with any of them.
what would be a better approach?
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

Peter Haggar discussed it's pro and con in his book "Practical Java", its listed as praxis #39 & #41.
[ April 07, 2003: Message edited by: Ryo Saeba ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You should always use arrays if you can. They are always more efficient than any Collection object for simply iterating through the array.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
thanks Thomas. i'll use array.
hi Ryo, can you please quote something important from the book as i don't have that book?
thank you.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Use array if you can is good advice for performance.
When can't you? What can Collections do that an array can't? Insert, grow, delete, sort, prevent duplicates come to mind. More?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
I think this is probably an example of premature optimization. Statistically, the bottleneck in your code will probably be somewhere else, and it won't matter much how good the performance of your iteration is. If it turns out that that iteration speed for your collection/array really is critical, and you don't need good performance when inserting/deleting, then yes, use an array (rewriting if necessary). But most of the time, I think the power and flexibility of the List interface makes it the preferred choice.
What can Collections do that an array can't? Insert, grow, delete, sort, prevent duplicates come to mind. More?
First, some more benefits to arrays. Contrary to the previous post, you can sort arrays by using the Arrays.sort() methods. If you're really motivated, you can even make a custom ArrayList-like implementation (unsing arrays of course) which can grow if needed, and is faster and more type-safe than an ArrayList, for a particular data type. E.g. if you've got a lot of ints to store, better to store them with an int[] array, rather than ArrayList's Object[] array, which also requires you to use a bunch of annoying wrapper objects. However, writing your own code to do this takes time, and unless you really know what you're doing, it will probably not be as good as ArrayList.
Advantages of Collections: it's easy to switch implementations later. You can easily replace an ArrayList with a LinkedList if you later realize that you need to to do a lot of insert/delete anywhere other than at the end of the structure. Or vice versa, if you realize that fast random access is more important than insert/delete. You can also take advantage of Collections methods to, say, create a read-only view of a collection (assuming the contained objcts are themselves immutable) or create a synchronized view of the collection (though this may well breed a false sense of security when thinking about thread safety). There are a number of other methods available in Collections but not Arrays. which can make some tasks much simpler for you - shuffle(), reverse(), min(), max(). I would argue that if you're not sure which data structure is most appropriate for your problem, using some sort of Colelction is usually the safest bet.
[ April 08, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Thanks for catching sort being wrong. And I get so fried with Java not allowing primitives in collections. It is such a pain to store a map of ints. Sheesh.
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Check out Primitive Collections for Java or GNU Trove4J. And, on a tangent because it's got nothing to do with primitives, I really like Jakarta Collections. Closures, predicates, and such simple but necessary things as a way to turn an Enumeration into an Iterator or to filter the results returned by an Iterator. All those things that are missing from the Java Collections framework.
- Peter
I agree. Here's the link:
subject: Use Enumeration/Iterator OR array?
Similar Threads
Enumerator Vs Iterator
Iterator or for loop
Iterator Vs Enumerator
style issue(maybe performance, too)
Collection API