This week's book giveaway is in the Performance forum.We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!

# ArrayList vs Vector Size and Capacity difference ?

Prabhat Ranjan
Ranch Hand
Posts: 397
Hi,

I have few sure douts that these conecpts are placed over net in many sites are wrong.

http://www.precisejava.com/javaperf/j2se/Collections.htm

1)
this statment is wrong. The initial size for ArrayList and Vector is 10.
As , ArrayList aList = new ArrayList();

System.out.println(" ArrayList Size " +aList.size()); gives output 0

Means initial size of ArrayList is 0.

While initial size of Vector is also 0 but initial capacity of vector is 10.

but you have to use the capacity methods of vector while there is no capacity method for ArrayList.
Vector aList = new Vector();

System.out.println(" ArrayList Size " +aList.capacity());

2) ArrayList increases its capacity by half approximately whenever its capacity reaches maximum (10) is wrong
but Vector increases its capacity by double whenever its capacity reaches maximum. thi is correct.

Stephan van Hulst
Bartender
Posts: 5897
63
Prabhat Ranjan wrote:1)
this statment is wrong. The initial size for ArrayList and Vector is 10.

They probably meant capacity. The initial capacity for ArrayList and Vector is 10. Capacity is different from size. Size is the number of elements currently stored. Capacity is the number of elements that can be stored before the data structure has to restructure its internal storage.

2) ArrayList increases its capacity by half approximately whenever its capacity reaches maximum (10) is wrong
but Vector increases its capacity by double whenever its capacity reaches maximum. thi is correct.

They're both wrong actually. Vector will increase its capacity by a value you can specify, the capacityIncrement. Only if this value is zero or less, will it double its internal storage when it gets full.
ArrayList doesn't specify how it increases storage.

Campbell Ritchie
Sheriff
Posts: 49411
62
You ought to look in the Sun/Oracle documentation.

Prabhat Ranjan
Ranch Hand
Posts: 397
Thanks for your reply , you are right these websites provides us wrong concept generally so many question are not fully answered from the even sun documentation and users try to find it in internet.

one point i want to highlight here ArrayList Capacity is 0 initially as ArrayList doesn't have method like capacity (but Vector has).

Stephan van Hulst
Bartender
Posts: 5897
63
• 1
The fact that it doesn't have a method to return the capacity does not mean the initial capacity is 0.

Matthew Brown
Bartender
Posts: 4567
8
Prabhat Ranjan wrote:one point i want to highlight here ArrayList Capacity is 0 initially as ArrayList doesn't have method like capacity (but Vector has).

Do you mean "capacity increment"? Because an ArrayList certainly doesn't have a zero initial capacity (it's 10 by default), and it does allow you to set it.

If you do, it's not really accurate to say that an ArrayList has a zero capacity increment, rather that it doesn't use the concept at all. Which is similar to setting the value to zero in a Vector, but that's just a convention.

Prabhat Ranjan
Ranch Hand
Posts: 397
you are right i have gone through the link you have provided me.

Its clearly written over there:

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.

An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation. This may reduce the amount of incremental reallocation.

i agree with you and you are absolutely correct, is there any way to find out or we can assure/convince that it has capacity 10 initially.

Stephan van Hulst
Bartender
Posts: 5897
63
Yes, by setting the initial capacity to 10.

Matthew Brown
Bartender
Posts: 4567
8
Well, if you don't trust the Javadocs for the no-arg constructor:
Constructs an empty list with an initial capacity of ten.

You can check the source code:

Prabhat Ranjan
Ranch Hand
Posts: 397
yes i agree and checked the code actually it is set to 10 .

only difference is that ArrayList doesn't have the capacity() Method while Vector has.

but the internal implementation is similar for both the classes to set the initial size to 10.

but what could be the reason with design prospective , Capacity method has not put inside the ArrayList ?

Santosh Kumar Nayak
Ranch Hand
Posts: 143
ArrayList increases its storage by using the below formula:-

OldCapacity*3/2+1

Winston Gutkowski
Bartender
Posts: 10425
63
Santosh Kumar Nayak wrote:ArrayList increases its storage by using the below formula:-
OldCapacity*3/2+1

You mean right now, on your machine and Java release it does; but I hope you never rely on it.

About the only thing you can say for certain (because it's hinted at in the documentation) is that the formula is exponential.

Winston

BTW: Is there any particular reason you're reviving old threads? It doesn't matter too much, but it's the second one in a row I've seen you reply to.