• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList vs Vector Size and Capacity difference ?

 
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prabhat Ranjan wrote:. . . please comments !

You ought to look in the Sun/Oracle documentation.
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The fact that it doesn't have a method to return the capacity does not mean the initial capacity is 0.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, by setting the initial capacity to 10.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?

 
Ranch Hand
Posts: 230
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ArrayList increases its storage by using the below formula:-

OldCapacity*3/2+1
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic