• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Vectors/Arrays/List?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What data structure is the fastest during runtime, and uses the least amount of resources (space)?
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vectors should be avoided. They are leftovers from the old days of Java.
As for the other two, it depends on what you are trying to do.
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To clarify just a bit. ArrayList is not synchronized. So if you have several users adding or removing data from the same ArrayList but from different threads you could have some trouble.
I believe List is synchronized.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe List is synchronized.
Probably you meant "I believe Vector is synchronized." List is an interface - some implementations are synchronized, and some are not. If you need synchronization, you still don't need Vector - put in the synchronized blocks yourself, or use Collections.synchronizedList() to get a synchronized view.
As far as theo original question, ignoring synchronization: if you don't have to resize the array/collection ever, then arrays are fastest and consume the least resources. If you do need to resize, you probably want ArrayList or LinkedList. If objects are only added or removed at the end of the list, use ArrayList; if they may be added or removed from the beginning or middle as well, use LinkedList. If you need to randomly access elements from the middle rather than beginning or end (i.e. access by index rather than sequentially with an iterator) then use an array or ArrayList rather than a LinkedList. If these guidelines contradict for your particular application, then try both and see which is faster. (Good to do this anyway if you have time.) See Tom's article here for more info.
 
Charmaine Gatling
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason I was thinking of using Vectors is simply because it's size is dynamically set instead of having to be set initially like an Array. The list is only used temporarily so I want to use a structure that not only use minimal space but doesn't effect execution time at all. Which one is better for this?
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
USe an ArrayList, not an array, if you want a dynamic sized container. Don't worry about performance unless performance is a problem.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
FYI: An array is the underlying data structure of a Vector and of an ArrayList. If you're curious about how that all works, I recommend taking a look at the source code (which hopefully can be found in a src.jar or src.zip file in your JDK installation).
 
reply
    Bookmark Topic Watch Topic
  • New Topic