What are the differences between ArrayList and a Vector

Showing Answers 1 - 75 of 84 Answers

satyanarayana.k

  • Jun 2nd, 2005
 

hi  
 
the basic differences are  
vector is a old collectiion came with jdk 1.0 or later. 
so, it comes under legacy classes. 
it allows synchronized way of accessing the elements. 
 
where as arraylist allows elements to be made synchronized as well asynchronized. 
its is dynamic collectiion. 
u have more flexibilty of using the arraylist collection. 
 
 
rgds 
satyanarayana

  Was this answer useful?  Yes

Rama Krishna

  • Jun 2nd, 2005
 

Both represent growable array of objects. The difference is Vector is Synchronized and Arraylist is not.

anoop

  • Jun 2nd, 2005
 

Vector is synchronized but arraylist is not.

  Was this answer useful?  Yes

Ramakrishna

  • Jul 9th, 2005
 

ArrayList will grow half the size what you initialise ..where as vector will grow doble the initial size..

  Was this answer useful?  Yes

srikanth

  • Jul 13th, 2005
 

one more difference apart from this is Vector also stores the objects but arraylist doesn't.......... 

  Was this answer useful?  Yes

Indu K. Vaghasia

  • Jul 15th, 2005
 

One Difference between Vector and ArrayList is In vector the data is retrive through elementAt() method while in ArrayList through get()

  Was this answer useful?  Yes

sri

  • Aug 4th, 2005
 

The default size of arraylist is 0 but for vector the default size is 10.

saurabh vaish

  • Aug 28th, 2005
 

vector has two advantages,first they can dynamically grow and second its method are synchronized.so if more then one object want to access your vector,no inconsistancy will appear

  Was this answer useful?  Yes

Himanshu Mendiratta

  • Aug 30th, 2005
 

1) Arraylist is not synchronized while vector is. 
2. Arraylist has no default size while vector has a default size of 10. 
 
Note: Methods in Vector are synchronised which means they are thread-safe and thus preclude access to the Vector elements by concurrent threads.Bu this imposes additional overhead on the JVM as it has to acquire and release locks on the vector objects under consideration. 
 
This is not possible in ArrayList since those methods are not synchronised and hence are faster in performance. 
 
Use Vector only if it will be accessed by multiple threads at a time else ArrayList is always better.

mohan

  • Sep 2nd, 2005
 

in ArrayList we can also store objects

  Was this answer useful?  Yes

usharani

  • Sep 7th, 2005
 

In ArrayList we can store similar data types  
but in Vectror we can store different types of data.

  Was this answer useful?  Yes

Sivalingan Babu

  • Sep 11th, 2005
 

ArrayList is not Synchronized, whereas Vector is synchronized. Array list is faster than Vector.

  Was this answer useful?  Yes

Narayanaa

  • Sep 14th, 2005
 

U should always us an ArrayList.The reason dates to JDK implementation over time. Vector is a legacy class in JDK which is still there for giving a backward compatability. The only self-expanding sequence in Java 1.0/1.1 was the Vector, so it saw a lot of use. Its flaws are too numerous to describe here. Basically, you can think of it as an ArrayList with long, awkward method names. Unfortunately, a lot of code was written using the Java 1.0/1.1 containers, and even new code is sometimes written using these classes. So although you should never use the old containers when writing new code, you?ll still need to be aware of themThe Java 1.0/1.1 version of the iterator chose to invent a new name, ?enumeration,? instead of using a term that everyone was already familiar with. The Enumeration interface is smaller than Iterator, with only two methods, and it uses longer method names: boolean hasMoreElements( ) produces true if this enumeration contains more elements, and Object nextElement( ) returns the next element of this enumeration if there are any more (otherwise it throws an exception).Enumeration is only an interface, not an implementation, and even new libraries sometimes still use the old Enumeration, which is unfortunate but generally harmless. Even though you should always use Iterator when you can in your own code, you must be prepared for libraries that want to hand you an Enumeration.In addition, you can produce an Enumeration for any Collection by using the Collections.enumeration( ) method. (Please see for the failfast mechanism of the iterators of the Collection framework below.)As of the Java 2 platform v1.2, this class has been retrofitted to implement List, so that it becomes a part of Java's collection framework. Unlike the new collection implementations, Vector is synchronized.The Java 2 containers represent a thorough redesign of the rather poor showings in Java 1.0 and 1.1. Some of the redesign makes things tighter and more sensible. It also fills out the functionality of the containers library, providing the behavior of linked lists, queues, and deques (double-ended queues, pronounced ?decks?)In any container class, you must have a way to put things in and a way to get things out. After all, that?s the primary job of a container?to hold things. In the ArrayList, add( ) is the way that you insert objects, and get( ) is one way to get things out. ArrayList is quite flexible; you can select anything at any time, and select multiple elements at once using different indexes.More compelleing reason for whcih we should not use the previous version classes i.e Hashtable, Vector, Stack whcih implements Vector is to do away with legacy classes in the newer inplementation. The concept of the stack was introduced earlier, with the LinkedList. What?s rather odd about the Java 1.0/1.1 Stack is that instead of using a Vector as a building block, Stack is inherited from Vector. So it has all of the characteristics and behaviors of a Vector plus some extra Stack behaviors. It?s difficult to know whether the designers consciously thought that this was an especially useful way of doing things, or whether it was just a na?ve design; in any event it was clearly not reviewed before it was rushed into distribution, so this bad design is still hanging around (but you should never use it).Apart from that the Java containers also have a mechanism to prevent more than one process from modifying the contents of a container. The problem occurs if you?re iterating through a container, and some other process steps in and inserts, removes, or changes an object in that container. Maybe you?ve already passed that object, maybe it?s ahead of you, maybe the size of the container shrinks after you call size( )?there are many scenarios for disaster. The Java containers library incorporates a fail-fast mechanism that looks for any changes to the container other than the ones your process is personally responsible for. If it detects that someone else is modifying the container, it immediately produces a ConcurrentModificationException. This is the ?fail-fast? aspect?it doesn?t try to detect a problem later on using a more complex algorithm.With a Collection, List, Set, or Map, the compiler still restricts you to calling only the methods in that interface, so it?s not like Smalltalk (in which you can call any method for any object, and find out only when you run the program whether your call does anything). In addition, most methods that take a Collection as an argument only read from that Collection?all the ?read? methods of Collection are not optional.This approach prevents an explosion of interfaces in the design. Other designs for container libraries always seem to end up with a confusing plethora of interfaces to describe each of the variations on the main theme, and are thus difficult to learn. It?s not even possible to capture all of the special cases in interfaces, because someone can always invent a new interface. The ?unsupported operation? approach achieves an important goal of the Java containers library: The containers are simple to learn and use; unsupported operations are a special case that can be learned later. For this approach to work, however: 1. The UnsupportedOperationException must be a rare event. That is, for most classes all operations should work, and only in special cases should an operation be unsupported. This is true in the Java containers library, since the classes you?ll use 99 percent of the time?ArrayList, LinkedList, HashSet, and HashMap, as well as the other concrete implementations?support all of the operations. The design does provide a ?back door? if you want to create a new Collection without providing meaningful definitions for all the methods in the Collection interface, and yet still fit it into the existing library. 2. When an operation is unsupported, there should be reasonable likelihood that an UnsupportedOperationException will appear at implementation time, rather than after you?ve shipped the product to the customer. After all, it indicates a programming error: You?ve used an implementation incorrectly. This point is less certain and is where the experimental nature of this design comes into play. Only over time will we find out how well it works. As far as the Synchronization is concerned 5.0 comes with java.util.concurrent package. You have many classes there which are tested and thread safe. They r built with Multi Threading built in. Its better that we should use instead of writing our own synchronization stuff.from java 1.2 we can create synchronized List usingList list = java.util.Collections.SynchronizedList(new ArrayList();But in 5.0 new package java.util.concurrent included in which it provides threadsafe Lists and sets

  Was this answer useful?  Yes

Nilang patel

  • Sep 16th, 2005
 

The main different is that ArrayList is object of type Collection and it is not syncronized while Vector is legacy class and synchronized

Nilang patel

  • Sep 16th, 2005
 

The main different is that ArrayList is object of type Collection and it is not syncronized while Vector is legacy class and synchronized

  Was this answer useful?  Yes

Nilang patel

  • Sep 16th, 2005
 

the most different is that ArrayList is not synchronized while the vector can synchornized.

  Was this answer useful?  Yes

s.n.p.c mahender

  • Sep 18th, 2005
 

array list list is static where we cant change the size of array.but where as vector is dynamic whose size increases or shrink dynmically as the program data storage requirement changes

  Was this answer useful?  Yes

Prasaath

  • Sep 18th, 2005
 

Arraylist is not synchornized where as vector is synchronized. Both are dynamic arrays which increase in size when elements are added

  Was this answer useful?  Yes

I Jetendra

  • Sep 19th, 2005
 

Instead, Array List can hold any primitive data as well as data elements but a Vector can hold object only. we need to cast the primitives data types using object wrappers.Vector can increase it size by itself public(vector(int initialcapacity, int incrementcapacity).

  Was this answer useful?  Yes

Geetha

  • Sep 23rd, 2005
 

ArrayList is not synchronized where as Vector is Synchronized

  Was this answer useful?  Yes

Dilpreet Singh

  • Sep 24th, 2005
 

srikanth Wrote: one more difference apart from this is Vector also stores the objects but arraylist doesn't.......... 

ArrayList can Store Objects
Check it Out!

  Was this answer useful?  Yes

A.J.Rameshkumar

  • Sep 26th, 2005
 

sri Wrote: The default size of arraylist is 0 but for vector the default size is 10.

The default size of arraylist also 10

vector also  default size is 10.

the only noe diff is vector is syncronized but

array list not syncronized...

even we can make that the arraylist also syncronized

by using collections.SyncronizedList.(new arrayList());


  Was this answer useful?  Yes

egate

  • Sep 29th, 2005
 

ArrayList and Vector both are grovable arrayArrayList:1.Not synchronized and thread safe2.Performance wise its faster than vectorVector:1.Its Synchronized and thread safe2.its not faster than arrayList

  Was this answer useful?  Yes

vijay sharma

  • Sep 30th, 2005
 

arraylist also stores objets.

 

  Was this answer useful?  Yes

swatiarr

  • Oct 2nd, 2005
 

Both are dynaming growing arrays.ArrayList is not synchronized where as vector is synchronized.

  Was this answer useful?  Yes

Raju

  • Oct 3rd, 2005
 

Hai,

Vector is growable array

Arraylist is not growable array

  Was this answer useful?  Yes

Lucky CHawla

  • Oct 4th, 2005
 

Ya its right Vector  is growable and Shrinkable coz when we initialise a vector object and add objects to it , its size 16 bytes more than what we added . While handling databases, vector are more flexible  to deal with . While ArrayList , is not much flexible with databases handling .Second,  The vector class has very good set functions to deal the length, deleting etc. Thanks

  Was this answer useful?  Yes

arun patel

  • Oct 6th, 2005
 

vector can grow

  Was this answer useful?  Yes

Sameer Shukla

  • Oct 6th, 2005
 

One more difference is there

Vector is synchronized , arraylist is not.

Thx

Sameer

  Was this answer useful?  Yes

Avinash

  • Oct 8th, 2005
 

In array List you can't enter the value or element after specifiying its index value while in the case of vector you can insert valueor element at any place of string at run time  

  Was this answer useful?  Yes

uvsurendra

  • Oct 9th, 2005
 

An array is an collection of primitive data types and it is static.

Vector is a collection object which stores objects and itis dynamic.

  Was this answer useful?  Yes

Ramu Neelam

  • Oct 10th, 2005
 

An Array is a Collection of elements which are primitive data type and it is static, where as a vector is a collection of objects(object references) and it is dynamic.

  Was this answer useful?  Yes

Seshu Ganugapati

  • Oct 10th, 2005
 

The main difference is Vector is sunchronized ArrayList is not. That means if multiple threads access arraylist, programmer need to write his own code to achieve synchronized access to its elements. Arraylist add, delete methods need to be synchronized externally.

  Was this answer useful?  Yes

prasad

  • Oct 17th, 2005
 

Arraylist supports iterator where as vector supports enumeration.

  Was this answer useful?  Yes

Ashok

  • Oct 18th, 2005
 

srikanth Wrote: one more difference apart from this is Vector also stores the objects but arraylist doesn't.......... 

  Was this answer useful?  Yes

santhi

  • Oct 19th, 2005
 

Array is a set of related data type and static whereas vector is a growable array of objects and dynamic.

  Was this answer useful?  Yes

seby jose

  • Oct 19th, 2005
 

Vector is synchronized, but ArrayList is not. 

  Was this answer useful?  Yes

santh

  • Mar 17th, 2006
 

what it meant  one more difference apart from this is Vector also stores the objects but arraylist doesn't.......... 

  Was this answer useful?  Yes

sara

  • Mar 23rd, 2006
 

ArrayList too can hold different type of objects.

  Was this answer useful?  Yes

arvind mulay

  • Aug 16th, 2006
 

how can u say so?

arraylist is a collection class.

and collection classes are made to store the objects.

so why cann't arraylist store the object?
if i am wrong, please let me know it with reason.

  Was this answer useful?  Yes

Siva

  • Feb 23rd, 2007
 

1. Arraylist is not synchronized while vector is.
2. Arraylist has no default size while vector has a default size of 10.
3. Arraylist doesn’t define any increment size while vector does.
4. Arraylist can be seen directly without any Iterator while vector requires an Iterator to                                                     display all it's content. (Not very sure).
5.In vector the data is retrieve through elementAt() method while in ArrayList through get() 

  Was this answer useful?  Yes

D.charan Raj

  • Mar 27th, 2007
 

array list list is static where we cant change the size of array.but where as vector is dynamic whose size increases or shrink dynmically as the program data storage requirement changes
 how can u say that array list cant change the size of an array

  Was this answer useful?  Yes

preethi

  • Jun 28th, 2007
 

is there any services available in tomcat5.5? if so, pz list out it

and how to make connection pooling in tomcat5.5

  Was this answer useful?  Yes

javednjaved

  • Apr 18th, 2008
 

Arraylist and vector are same but the difference is vector is legacy class and synchronised but arraylist is not. every time of insert in vector, it creates double the size of its default.

  Was this answer useful?  Yes

sanjay637

  • May 27th, 2008
 

1  . ArrayList gives better performance tha Vector.
    since the methods off vector are synchronized .it increases overhead on jvm.
    where as the methods of ArrayList or not Sysnchroized.

2.for retriving data  use     get()             method                   while using           ArrayList
                                          elementAt() method               while using           Vector
3.initial or default size of ArrayList is  0
                                        Vector it is 10.



thanks
Naveen Kumar

  Was this answer useful?  Yes

Mr Sterling

  • Feb 16th, 2009
 

Vector and ArrayList: Both are having their own importance. And as per your requirement, you should go for one. In max cases - It is suggested to use ArrayList over Vector.

  Was this answer useful?  Yes

narraa

  • Jun 29th, 2009
 

1. Arraylist is not synchronized while vector is.

2. Arraylist has no default size while vector has a default
size of 10.

3.we can specify the increment size with the vector and with
array list we cannot incresing capacity in vector using with ensureCapacity(int x) method

4. Array list and Vector have one in common. They both implement Random Access.

5. It is possible to provide synchronization using Array List
by using the utility methods in Collection.

  Was this answer useful?  Yes

vermasoftit

  • Jun 30th, 2009
 

Arraylist is resizable - array implementation of the list interface. , this class provides methods to manipulate the size of the array that is used internally to store the list.(This class is roughly equivalent to Vector, except that it is unsynchronized.)

Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.

The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.

Each vector tries to optimize storage management by maintaining a capacity and a capacityIncrement. The capacity is always at least as large as the vector size; it is usually larger because as components are added to the vector, the vector's storage increases in chunks the size of capacityIncrement. An application can increase the capacity of a vector before inserting a large number of components; this reduces the amount of incremental reallocation.
 
Vector is synchronized.
Default size is ten .

  Was this answer useful?  Yes

Sriram_Geek

  • Aug 21st, 2010
 

As others have stated, Vector by default is Synchronized where as ArrayList is not Synchronized. Synchronized meaning only one one thread can access it at a time. So if there are multiple threads accessing, it is better to use a Vector.


Having said this, ArrayList can also be synchronized using certain algorithms provided by Collections.

Also please note, both ArrayList and Vector can grow dynamically in size.

Also please not Vectors do not always double their size when the size is exceeded. It all depends on the implementation. This is because Vector provides 4 constructors:

1) Vector()
2) Vector(int size)
3) Vector(int size, int increment)
4) Vector(Collection c)

If the default constructor is used then a Vector of size 10 is created. If more than 10 elements are added, then the vector size would double. However this can be prevented by using the third constructor above Vector(int size, int increment). Increment value specifies by how much you want the Vector size to grow when the length exceeds the pre-defined value.

With this said there are only two differences between a Vector and an ArrayList. They are Vectors are synchronized by default, whereas Arraylist are not. Second being with Java 2, Vectors have been made to extend AbstractList class which implements List interface. Hence Vectors provide additional legacy methods hasMoreElements() and nextElement() etc.

Rega

  Was this answer useful?  Yes

sapanindia

  • Aug 23rd, 2010
 

ArrayList:- when we create an array we allocate all the memory while initialize the array, so If we had an array of 100 objects, we will allocate space to hold 100 object reference as soon as we initialize the array.

Vector:- vector class provides the capability to implement a growable array of object.

arraylist -Not synchronized and length space
vector -synchronized and not length space
Example:

While if not multiple thread are going to access the same instance then use Arraylist ie not thread safe.

Thus whenever there is a possibility of multiple threads accessing the same instance one should use Vector. i.e thread safe

  Was this answer useful?  Yes

ramesh7214

  • Apr 23rd, 2011
 

vector is the legacy class nothing but old class it contains the all the synchronized methods.
that mean thread safe method.
using the vector in all projects are not good.
arraylist contains the asynchronized methods and it is newly implemented collection.
using the arraylist is better than the using the vector.

  Was this answer useful?  Yes

YUVARAJ

  • May 11th, 2012
 

Array is a set of related data type and static whereas vector is a grow able array of objects and dynamic

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions