Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

differences between Vector and ArrayList

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15485
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic