Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vectors/Arrays/List?

 
Charmaine Gatling
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What data structure is the fastest during runtime, and uses the least amount of resources (space)?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • 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.
 
Greg Brouelette
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • 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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • 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
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • 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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • 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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic