wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes ArrayList vs Vector Size and Capacity difference ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList vs Vector Size and Capacity difference ?" Watch "ArrayList vs Vector Size and Capacity difference ?" New topic
Author

ArrayList vs Vector Size and Capacity difference ?

Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
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.

please comments !
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3582
    
  14

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.

please comments !

Forget about this article. It's hopelessly outdated.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
Prabhat Ranjan wrote:. . . please comments !
You ought to look in the Sun/Oracle documentation.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
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

Joined: Sep 20, 2010
Posts: 3582
    
  14

The fact that it doesn't have a method to return the capacity does not mean the initial capacity is 0.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    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

Joined: Oct 04, 2006
Posts: 391
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

Joined: Sep 20, 2010
Posts: 3582
    
  14

Yes, by setting the initial capacity to 10.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    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

Joined: Oct 04, 2006
Posts: 391
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

Joined: Aug 02, 2011
Posts: 92
ArrayList increases its storage by using the below formula:-

OldCapacity*3/2+1
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7529
    
  18

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.

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
jQuery in Action, 2nd edition
 
subject: ArrayList vs Vector Size and Capacity difference ?
 
Similar Threads
Can we decrease the size of the ArrayList ?.
differences between Vector and ArrayList
how to prevent vector to be with 10 elements as defult ?
Difference between ArrayList and Vector