aspose file tools*
The moose likes Java in General and the fly likes Difference between ArrayList and Vector 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 » Java in General
Bookmark "Difference between ArrayList and Vector" Watch "Difference between ArrayList and Vector" New topic
Author

Difference between ArrayList and Vector

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
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
Marshal

Joined: Mar 22, 2005
Posts: 42275
    
  64
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?


Ping & DNS - my free Android networking tools app
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

@Rob
What does synchronisation means in vectors..can you please elaborate. I am not getting it.

thanks


Good luck!!
A small leak can sink a Gigantic ship.>
Eswar Nec
Ranch Hand

Joined: May 02, 2010
Posts: 105
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.


Wake up! Don't let your smile be snatched away by anybody!
Regards, Eswar
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

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

Joined: May 02, 2010
Posts: 105
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

Joined: May 02, 2010
Posts: 105
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

Joined: Mar 05, 2008
Posts: 3018
    
  10
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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

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

Joined: Mar 05, 2008
Posts: 3018
    
  10
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

Joined: May 02, 2010
Posts: 105
Hi Rob Prime,

Cool. Thanks For your nice reply.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

You're welcome
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between ArrayList and Vector