aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes vectors! 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "vectors!" Watch "vectors!" New topic
Author

vectors!

ashok khetan
Ranch Hand

Joined: Jul 29, 2001
Posts: 153
hi! can u pl explain the output of this code?
thanx.
ashok.
_______________
import java.util.*;
class Tom {
public static void main(String args[]) {
Vector v = new Vector(1);
System.out.println(v.capacity());
v.add("ashok");
v.add("vikas");
v.add("john");
System.out.println(v.capacity());
}
}
Roopa Bagur
Ranch Hand

Joined: Nov 03, 2000
Posts: 267
Vectors add method
Appends the specified element to the end of this Vector.
In your program you created a vector with capacity 1 but you added 3 later on..so that makes it 4.
That the reason for the 2nd system out to print 4.
Correct me if I am wrong.
Roopa.

Originally posted by ashok khetan:
hi! can u pl explain the output of this code?
thanx.
ashok.
_______________
import java.util.*;
class Tom {
public static void main(String args[]) {
Vector v = new Vector(1);
System.out.println(v.capacity());
v.add("ashok");
v.add("vikas");
v.add("john");
System.out.println(v.capacity());
}
}

Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
If you change creation of the new Vector to: new Vector() the println will be 10 and 10. The question is testing your knowledge of capacity and size methods and what they return. The initial capacity of Vector is 10 if not otherwise stated. The size is the number of elements in the Vector.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Ashok,
It also shows that the Vector capacity increment is 1 unless otherwise specified. For example, we could have the following:
Vector v = new Vector( 1, 5 );
in the original code shown. Then the second println would have printed 6. Once we got past 1, the capacity would have grown by 5 elements.
Regards,
Manfred.
galen wang
Greenhorn

Joined: Sep 21, 2001
Posts: 13
when using add() and there is no room in the Vector, then the capacity will be increased. the rule is:
1. if the Vector is created by no parameter (e.g. Vector v = new Vector()) then the capacity will be doubled based on 10. that is 2*10, 2*2*10, 2*2*2*10, .... (2^i * 10)
2. if the Vector is created by one parameter (e.g. Vector v = new Vector(n)) then the capacity will be doubled based on n. that is 2*n, 2*2*n, 2*2*2*n, .... (2^i * n)
for n = 1, it will be 2,4,8,18...
for n = 2, it will be 4,8,16,32...
for n = 3, it will be 6,12,24,48...
...
3. if the Vector is created by two parameter (e.g. Vector v = new Vector(l, m)) then the capacity will be increased by the size. that is 2*(m-l+1), 3*(m-l+1), 4*(m-l+1) .... (i*(m-l+1))
for n = 1, it will be 1,2,3,4...
for n = 2, it will be 2,4,6,8...
for n = 3, it will be 3,6,9,12...
...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: vectors!