scenario for arrayList and Vector
Hi Folks,
I am very confused about the difference b/w arrayList and vector. I had read the material available on net and previous questions in this fourm. But still i did not get the clear picture of difference b/w vector and arrayList.
Here is my code
Vector v =new Vector();
ArrayList a =new ArrayList();
v.add("krishna");
v.add("rama");
a.add("asdfa");
a.add("asdfa");
System.out.println("v size"+v.capacity());
System.out.println("a size"+a.size());
v.add("pad");
a.add("adfas");
System.out.println(" after v size"+v.capacity());
System.out.println(" after v size"+v.size());
System.out.println(" after a size"+a.size());
I know that vector capacity will be doubled when we reach the maximum size of vector.
What is the case if we reach arraylist maximum size? Does the capacity will be doubled?.
I know that vector is synchronized and arraylist is not synchronized.
But still we can make arrayList as synchronized by using
Collection.synchronizedList().
In this scenario, how to distinguish whether we have to go for arrayList (or) vector?
How does we know that vector has allocated this much amount of memory and also for arraylist?
regards,
RamP
[1659 byte] By [
Rampa] at [2007-11-26 16:30:54]

if we need our application to run fast we use arraylist
why can't we make our application using vector?
Rampa at 2007-7-8 22:55:23 >

Of course you can but Vector is always synchronized and has therfore severe overhead.
ok. What about arraylist capacity when it reaches the maximum size?" vector capacity will be doubled when we reach the maximum size of vector."
Rampa at 2007-7-8 22:55:23 >

From the API spec: The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.
> I am very confused about the difference b/w
> arrayList and vector.
Don't be. Both are equivalent in that they both implement the List interface.
The main differences are that Vector is synchronized which ArrayList is not (but can be made). They also have different growth characteristics. In Vector you can control the growth strategy to a certain extent but in ArrayList you cannot.
In my view the ArrayList is the better choise in most cases.
- It belongs to the "modern" collections.
- It's not synchronized so if you don't need that you don't pay for it.
- And it has a growth strategy which has proven adequate for most uses so you don't have to bother.
Thanks for your reply embla and Mortan.
I had read some where that If allocate 5 elements to vector and add 2 elements like the following, memory is allocated for all the 5 elements.
vector v = new vector(5);
v.add("test");
v.add("test");
But in arrayList , this is not the case. If you allocate memory for 5 elements using arraylist and add 2 elements to the arraylist, memory is allocated for 2 elements.
ArrayList a = new ArrayList(5);
a.add("atest");
a.add("atest");
Am I correct?
Rampa at 2007-7-8 22:55:23 >

>Am I correct?
No according to the specification you have an intial capacity of 5 in both cases. This means that there will be allocated space for 5 elements initially. The data structurs will grow when you add the 6'th element. The Vector will grow to a capacity of 10 elements (it doubles). For the ArrayList you don't know because it's not specified. In the Sun implementation though I think I recall it will be 1.8*5 = 9 elements (a growth factor of 1.8 instead of Vector's 2.0)
If you're very concerned about having excess capacity note that both ArrayList and Vector sport a trimToSize method you can use to trim away excess capacity when loading is finished. I wouldn't spend too much time bothering about this though especially not when you store just a few elements.
Did you know that in Java object values are not stored in the collections. Only references (pointers) to the objects are held by the collections. So the collections themselves are usually fairly small compared to the space the actual object values occupy.
Rampa at 2007-7-8 22:55:23 >

//. In the Sun implementation though I think I recall it will be 1.8*5 = 9 elements //(a growth factor of 1.8 instead of Vector's 2.0)
I had made check in the java source code. There is small change in variation of computing capacity.
//Did you know that in Java object values are not stored in the collections. Only references (pointers) to the objects are held by the collections.
Then where will actually the values are stored? can you explain little bit clearly.
Rampa at 2007-7-8 22:55:23 >

> //. In the Sun implementation though I think I recall
> it will be 1.8*5 = 9 elements //(a growth factor of
> 1.8 instead of Vector's 2.0)
> I had made check in the java source code. There is
> small change in variation of computing capacity.
Note that because the growth factor isn't part of the ArrayList specification it really doesn't matter. Sun and say IBM can use different growth factors in their implementation and it can change anytime in the future.
> //Did you know that in Java object values are not
> stored in the collections. Only references (pointers)
> to the objects are held by the collections.
>
> Then where will actually the
> values are stored? can you explain little bit
> clearly.
If you add an object to an ArrayList it's not a copy of the object that get's stored in the ArrayList, it's a reference to the object. The ArrayList will store a pointer to the actual object value.
All "object variables" in Java are in fact object reference variables. The object values themselves are stored in a section of memory called the heap. Variables just hold references pointing at objects on the heap. Reference variables can be part of an object value and then the variable will be on the heap too but still, it will contain a reference to another object on the heap and not the object value itself.
Rampa at 2007-7-8 22:55:23 >

http://tv.sureshvasudev.com