Regarding vector

hello,If a declare a vector likeVector v=new vector(5,10);when the size of the vector will increament.After adding 5th element.or befor adding 5th element.What will be the size of the vector at this time.Bipin.V
[253 byte] By [bipina] at [2007-11-26 15:48:09]
# 1
15
manuel.leiriaa at 2007-7-8 22:07:39 > top of Java-index,Java Essentials,Java Programming...
# 2
when u will add element after 5th element...size will increase by 10....that is final size will be 15
Vikash.SunJavaa at 2007-7-8 22:07:39 > top of Java-index,Java Essentials,Java Programming...
# 3
Actually it should increase when adding the sixth element, so the Vector's size still is 5 before and after adding the fifth element.
quittea at 2007-7-8 22:07:39 > top of Java-index,Java Essentials,Java Programming...
# 4
Hi, If you are not sure , you can calculate and display the size of vector. v.length and v.size()these two will give you length as well as size of vector.RegardsNikhil
nikhil_shravanea at 2007-7-8 22:07:39 > top of Java-index,Java Essentials,Java Programming...
# 5

> v.length

... which does not even exist

> and v.size()

... which only gives the number of elements added, not the internal size.

If you're really interested in what happens in a Vector, you can try this:

import java.util.*;

public class Veccy extends Vector {

public Veccy(int a, int b) {

super(a, b);

}

public static void main(String[] args) {

Veccy v = new Veccy(5, 10);

v.add("a");

v.add("b");

v.add("c");

v.add("d");

v.add("e");

System.out.println(v.elementData.length);

}

}

quittea at 2007-7-8 22:07:39 > top of Java-index,Java Essentials,Java Programming...
# 6

see....if u add sixth element v.size() will return size as 6.....

Now the statement (Veccy v = new Veccy(5, 10);) says OS that he will create 5 objects and if i add sixth element please reserve me 10 more spaces to me....

Now, when u add 6th element the OS has declared 10 spaces for you but only after u enter 6th element it will assign 6th place for your element....rest 9 places you cannot claim....It is similar to the fact that when you declare any object and print it's size it returns 0.....only when you define that object OS reserves apace for it....thus giving you actual size

Similar is this case.....I guess u have got the hint...

Vikash.SunJavaa at 2007-7-8 22:07:39 > top of Java-index,Java Essentials,Java Programming...
# 7

> see....if u add sixth element v.size() will return size as 6.....

Yes ...

> Now the statement (Veccy v = new Veccy(5, 10);) says

> OS that he will create 5 objects

This statement does not create any objects besides the Vector (and its internal fields).

> and if i add sixth element please reserve me 10 more spaces to me....

Right ... In general, it means "each time there's not enough space for a new element, do not only reserve 1 new place but 10 in a single step" (cause this is more efficient).

> Now, when u add 6th element the OS has declared 10 spaces for you

15 spaces as a total, in this case. And the OS is not necessarily involved directly to this step, as the JVM is the first address to talk to.

> but only after u enter 6th element it will assign 6th place for your element....

Yes ...

> rest 9 places you cannot claim....

Of course you can ?by adding more objects.

> It is similar to the fact

> that when you declare any object and print it's size

> it returns 0.....only when you define that object OS

> reserves apace for it....thus giving you actual size

I'm not sure whether you got the difference between the result of a call to size() and the internal space allocated by the Vector, which is >= size(). Typically, if you create a Vector instance without giving an explicit initial size, there's a default space of 10 elements reserved.

quittea at 2007-7-8 22:07:39 > top of Java-index,Java Essentials,Java Programming...