Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between ArrayList and Vector

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Arraylist is not synchronized while vector is. (Correct)
2. Arraylist has no default size while vector has a default size of 10. (Is this correct , I have a doubt)
3. Arraylist don't define any increment size while vector does. (Correct)
4. Arraylist can be seen directly without any iterator while vector requires an iterator to display all it's content. (not very sure).


For Point # 2, In the api i have :
ArrayList()
Constructs an empty list with an initial capacity of ten.

Vector()
Constructs an empty vector so that its internal data array has size 10 and its standard capacity increment is zero.

Can someone please help me with point 2 and 4
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For #2, check out the source code of the ArrayList class (it's in a file called "src.zip" that is somewhere inside of your JDK installation directory).

As to #4, look through the javadocs of the Vector class - is there a method that returns the element at a particular index?
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jose chiramal wrote:1. Arraylist is not synchronized while vector is. (Correct)

Correct.

2. Arraylist has no default size while vector has a default size of 10. (Is this correct , I have a doubt)

Neither has a default size (well, 0 can be considered as the default size) but both have a default capacity of 10, as you've already quoted. (ArrayList's Javadoc says ten.)
3. Arraylist don't define any increment size while vector does. (Correct)

Correct. However, if left unspecified a Vector grows with factor 2 whereas ArrayList grows with factor 1.5.

4. Arraylist can be seen directly without any iterator while vector requires an iterator to display all it's content. (not very sure).

What do you mean with "directly seen"? Both have a toString() method to turn itself into a similar looking String (in fact, both inherit their implementation from AbstractList, although Vector makes it synchronized). Both have size() and get(int) methods to query the size and any element.
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Rob
What does synchronisation means in vectors..can you please elaborate. I am not getting it.

thanks
 
Eswar Nec
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList has no default size.

Vector has a default size of 10.


One more Difference:

The ArrayList increases its array size by 50 percent.

A Vector defaults to doubling the size of its array.

 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eswara Moorthy Nec wrote:ArrayList has no default size.

Vector has a default size of 10.

Nonsense. The size is the number of elements stored inside the List, the capacity is the size of the backing array. Both have a default size of 0 and a default capacity of 10. Both are clearly specified in the API, as "empty list" / "empty vector" indicates a size of 0.
 
Eswar Nec
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
Eswara Moorthy Nec wrote:ArrayList has no default size.

Vector has a default size of 10.




The Output is:

ArrayList Intial Size : 0
Vector Intial size is : 0
Vector Default Capacity : 10

The default capacity of vector is 10.
 
Eswar Nec
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
NonsenseThe size is the number of elements stored inside the List, the capacity is the size of the backing array. Both have a default size of 0 and a default capacity of 10. Both are clearly specified in the API, as "empty list" / "empty vector" indicates a size of 0.


Hello Mr.Rob Prime
You are not author of java language .Don't use the wrong word...
This is public forum. Not your own.
You just mention 'this is wrong answer' or 'This is right answer'. thats all.
 
Mike Simmons
Ranch Hand
Posts: 3032
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eswara Moorthy Nec wrote:You are not author of java language .Don't use the wrong word...
This is public forum. Not your own.
You just mention 'this is wrong answer' or 'This is right answer'. thats all.

This is wrong answer.

That is all.
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you feel offended by my use of the word nonsense then I apologize. It's just a word I use in daily life when someone is wrong.
 
Henry Wong
author
Marshal
Pie
Posts: 20991
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:If you feel offended by my use of the word nonsense then I apologize. It's just a word I use in daily life when someone is wrong.


I have also noticed that the word "nonsense" seem to be taken more seriously than intended. It does make sense if you take the literal definition of the word. And I am sure that there are places where that word is to be used when the intention is to offend.

Where I am from (Brooklyn, New York), it just means "strongly disagree"... but considering the history, I too, will refrain from using that word.

Henry
 
Paul Clapham
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eswara Moorthy Nec wrote:One more Difference:

The ArrayList increases its array size by 50 percent.

A Vector defaults to doubling the size of its array.



I always thought it was the other way around. But in fact neither is true; the API documentation for ArrayList says:
The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.
And the API documentation for Vector says:
as components are added to the vector, the vector's storage increases in chunks the size of capacityIncrement.
Of course, particular implementations may work in particular ways, and what you say may be true of one Java implementation. Other Java implementations may work differently.
 
Mike Simmons
Ranch Hand
Posts: 3032
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:...And the API documentation for Vector says:
as components are added to the vector, the vector's storage increases in chunks the size of capacityIncrement.

For Vector it also says:
If the capacity increment is less than or equal to zero, the capacity of the vector is doubled each time it needs to grow.

Since the capacity increment defaults to zero, that means Vector doubles by default, according to the API.

The whole idea of a fixed capacity increment was a bad idea anyway in my opinion, as it leads to poor performance for large N. So it's good that ArrayList stopped using it.
 
Eswar Nec
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob Prime,

Cool. Thanks For your nice reply.

 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
few of java developers are very confused about the word "Capacity" and "Size" in context of Vector and ArraytList.

For ArrayList and Vector , default initial size = 0
while for Vector the default Capacity = 10

So , please understand that size and capacity is different thing.

Capacity is something like buffer or backing size.
while size , if we add any element then size grows.

I think most of interviewers also confused about this and ask this Question , what is initial size of vector and expecting the answer is size=10(While it is the capacity of the vector 10).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic