hey all, let's say i have a vector and i wanna iterate through it until the needed object in this vector is found. i tried both : an iterator and a loop with vector's getter . the second choice looks much faster. if the iterator is not as fast as getter, why would one use it? thanks
There are a few reasons that I can think of, other probably can tell more. 1. Abstraction. Iterator work with all Collection, which consist of List and Set interface and their implementations. getter only work with List only. Same reason it is suggested that you declare your variable as Collection object instead of instance of ArrayList or Vector for example. So you can switch implementation easily. 2. Safety reason. with direct access using get() and index, it is easier to make mistake and pas invalid/out-of-bound index. 3. Extra features. Iterator and ListIterator has other methods that help to do other thing beside traversing the collection, like inserting or removing object. So you don't have to do it manually by calculating the index, etc. Check out this post, Jim has more explanation: more on iterator [ October 05, 2003: Message edited by: Wirianto Djunaidi ]
Hi, normally you use an iterator if you want to iterate over the complete content of a collection etc. If you know which element to take and the class is providing a getter method then take the getter method.
Originally posted by Wirianto Djunaidi: getter only work with List only.
And on a LinkedList, using an iterator actually is *much* faster.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
An iterator also provides fail-fast behavior if the list you're iterating over is sturcturally modified while you're iterating. (I.e. if another thread adds or removes elemenst from the collection.) Fail-fast means that if the iterator detects that the list is being modified by another thread while you're iterating, it will promptly throw a ConcurrentModificationException. This is usually a good thing, because it make it easier for you to detect the problem and fix it (typically by introducing synchronization, or iterating over a copy of the collection rather than the original). If you use get() instead, you may well have a program that functions correctly 50%, 90%, maybe 99% of the time - but then it will fail in strange and mysterious ways. The failures may be hard to preproduce or trace the cause of. If my program is going to have a bug, I'd rather it be a big obvious bug that I can find easily, rather than a small subtle bug the appears unpredictably. So to me, this is a big advantage to using an Iterator when dealing with a Collection in a multi-threaded environment (or an environment that may become multi-threaded later; this can be hard to anticipate).