• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

On what circumstances the ArrayList, LinkedList, Vector will be used.

 
ssantha kumar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On what circumstances the ArrayList, LinkedList, Vector will be used?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49841
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use Vector if you can help it. Use ArrayList instead. Have you been through the Java Tutorial about collections? You will find more details in there.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To answer in short, when you want to deal with heterogeneous elements in one container, that's why Collections are there and the aforementioned elements are all a part of Collections in Java.

The term heterogeneous means elements of different/dissimilar kind which is contradicting to arrays as arrays can only store the elements of same kind.

You may go through the link given by Campbell for more detailed information.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Raghavan Muthu]: To answer in short, when you want to deal with heterogeneous elements in one container, that's why Collections are there

Nonsense. Collections are there to deal with groups of elements, either heterogeneous or homogeneous. I would argue that they're much more effective at dealing with homogeneous elements. Sure, it's possible to create heterogeneous collections. But why? It's certainly possible to to create homogeneous collections as well:

See? Easy.

To consider the original question:

ArrayList is generally what you should use in most cases when you want some sort of collection.

LinkedList is a good alternative if you are frequently inserting or deleting from the beginning of the list, or from the middle (using an iterator).

Vector is used only by unwashed savages. Books that recommend Vector should be simply ignored - they are too old to be useful. Anyone who attempts to describe a Vector as "thread-safe" should be shot on sight. Seriously, these people are idiots, and should be kept as far as possible from working coders.

If you have an ancient textbook which uses the term Vector, simply pretend that they said ArrayList instead. Ignore any discussion of how Vector is "thread-safe", as the author doesn't know what he's talking about - otherwise, Vector can be easily replaced with ArrayList.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jim.

Obviously, they are here to "deal with the group of elements" precisely as the term 'Collection' literally means.

I agree that if at all you have a facility to deal with heterogeneous elements, that indeed facilitates for homogeneous elements as well. But as such, the importance of Collections were said to deal with heterogeneous elements right?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, but I really don't understand what you mean here. If dealing with heterogeneous elements was particularly important, an Object[] array did that well enough before Collections were around. Yes, Collections can deal with heterogeneous types, true, but I don't see why anyone should consider this terribly important.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic