> 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);
}
}
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...
> 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.