Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Difference between ArrayList and Vector

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22579
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 247
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Rob
What does synchronisation means in vectors..can you please elaborate. I am not getting it.

thanks
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22579
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 4071
56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22579
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 26966
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 4071
56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob Prime,

Cool. Thanks For your nice reply.

 
Rob Spoor
Sheriff
Posts: 22579
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
reply
    Bookmark Topic Watch Topic
  • New Topic