aspose file tools*
The moose likes Beginning Java and the fly likes Vectors/Arrays/List? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Vectors/Arrays/List?" Watch "Vectors/Arrays/List?" New topic
Author

Vectors/Arrays/List?

Charmaine Gatling
Greenhorn

Joined: Jan 10, 2002
Posts: 11
What data structure is the fastest during runtime, and uses the least amount of resources (space)?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Greg Brouelette
Ranch Hand

Joined: Jan 23, 2002
Posts: 144
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.


For a good Prime, call:<br />29819592777931214269172453467810429868925511217482600306406141434158089
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Charmaine Gatling
Greenhorn

Joined: Jan 10, 2002
Posts: 11
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

Joined: May 05, 2000
Posts: 13974
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

Joined: Dec 10, 2001
Posts: 7023
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).


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Vectors/Arrays/List?