permaculture playing cards*
The moose likes Beginning Java and the fly likes differences between Vector and ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "differences between Vector and ArrayList" Watch "differences between Vector and ArrayList" New topic
Author

differences between Vector and ArrayList

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 371
1. Arraylist has no default size while vector has a default size of 10.

2. Arraylist don't define any increment size while vector does.
What is increment size ?

3. Arraylist can be seen directly without any iterator while vector requires an iterator to display all it's content.
Is this true that vactor can not be accessed without using iterator ?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

nirjari patel wrote: 1. Arraylist has no default size while vector has a default size of 10.

2. Arraylist don't define any increment size while vector does.
What is increment size ?

3. Arraylist can be seen directly without any iterator while vector requires an iterator to display all it's content.
Is this true that vactor can not be accessed without using iterator ?


From where did you find these differences?

This link explains the differences.


Mohamed Sanaulla | My Blog
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

nirjari patel wrote:1. Arraylist has no default size while vector has a default size of 10.

That's wrong; it confuses size with capacity. When you create an ArrayList or a Vector with their no-arguments constructors, they will both have a size of 0 - there are no elements in the ArrayList or the Vector.

Both ArrayList and Vector internally use an array to store their elements. When you keep adding elements, the internal array will be full at a certain moment, and the ArrayList or Vector will have to allocate a bigger array, copy all the existing elements to the new array, and then add the new element. The size of the internal array is the capacity of the ArrayList or Vector; the number of elements that are really stored is the size.

What the initial capacity is and what exactly the strategy is for growing the array is an implementation detail that is explicitly not specified. This is what the API documentation of ArrayList says:
java.util.ArrayList wrote: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.

So, maybe the initial capacity of an ArrayList is zero, but that's not something that's officially specified, and in other implementations of Java besides Oracle's it might be different.

nirjari patel wrote:2. Arraylist don't define any increment size while vector does.

That's exactly what the quote above from the API documentation says.

The API documentation of Vector says:
java.util.Vector wrote:Each vector tries to optimize storage management by maintaining a capacity and a capacityIncrement. The capacity is always at least as large as the vector size; it is usually larger because as components are added to the vector, the vector's storage increases in chunks the size of capacityIncrement. An application can increase the capacity of a vector before inserting a large number of components; this reduces the amount of incremental reallocation.

So indeed, for a Vector it is possible to specify the increment - for ArrayList it's all internal and you can't specify it.

nirjari patel wrote:3. Arraylist can be seen directly without any iterator while vector requires an iterator to display all it's content.

I don't understand what is meant by this. Both ArrayList and Vector have other ways to get at the elements without using an Iterator, they for example both have a get() method, and they both implement Iterable, which means that they can be used with the for-each loop syntax.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
Don't get me started about those stupid light bulbs.
 
subject: differences between Vector and ArrayList