Whats the fastest way to traverse all the elements of a Vector?
Location: http://www.jguru.com/faq/view.jsp?EID=33288 Created: Apr 6, 2000 Modified: 2001-11-29 22:40:29.604 Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7) If speed is of the essence, do not use a Vector. Instead, use an ArrayList. All accesses to a Vector are synchronized, whether you need it or not. If you know you aren t accessing the structure from multiple threads, the ArrayList will be much faster. With that said, what about if you need to stay with a Vector? There are at least four ways to traverse through all the elements of a Vector: • Using a for loop and accessing each element with elementAt(index) or get(index) • Using an Emumeration • Using an Iterator • Using an Enumeration/Iterator, but relying on an exception to catch ending Of the four, there are neglible differences in performance. While looping through to a specific index is a little faster, you lose the flexibility of changing to another data structure at a later time. With the Enumeration / Ite