wood burning stoves 2.0
The moose likes Beginning Java and the fly likes Arrays, Arraylist and vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arrays, Arraylist and vector" Watch "Arrays, Arraylist and vector" New topic

Arrays, Arraylist and vector

Chandan V. Singh

Joined: Sep 14, 2005
Posts: 1
Greeting to all..

my question is :
among Arrays, Arraylist and vector; which one would be the best choice and under what circumstance ?

Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

among Arrays, Arraylist and vector; which one would be the best choice and under what circumstance ?

The "best" choice in my opinion would be the one that allows you to accomplish your programming goal(s) in an effective, efficient, maintainable manner. As such, the choice is dependent on the situation (though I'd generally only use Vector when I was forced to by the API, not by choice).

Prefer ArrayList to Vector. Both offer the same interface (i.e., List), but Vector is an outdated class that was shoehorned into the Collection API. If you absolutely need to have your List synchronized, use Collections.synchronizedList().

The Java� Tutorial - Arrays
Java� Collections Framework
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195


Welcome to JavaRanch!

A very condensed guide:

Use an array if you're working with a known, fixed number of elements of a uniform type. Use an ArrayList if the number of elements is variable or not known in advance. Use a LinkedList if you have a list which to which you'll frequently add elements in the middle or delete elements from the middle.

Don't use Vector in new code, ever. ArrayList is intended as its replacement.

[Jess in Action][AskingGoodQuestions]
It is sorta covered in the JavaRanch Style Guide.
subject: Arrays, Arraylist and vector
It's not a secret anymore!