Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Whats the fastest way to traverse all the elements of a Vector?

0
Posted

Whats the fastest way to traverse all the elements of a Vector?

0

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

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123