programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Jeanne Boyarsky
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Carey Brown
• Frits Walraven
Bartenders:
• Piet Souris
• Himai Minh

# Adding an element in a Vector... ???

Ranch Hand
Posts: 18944
• Number of slices to send:
Optional 'thank-you' note:
What is the effect of adding the sixth element to a vector created in the following manner:

new Vector(5, 10);
A.An IndexOutOfBounds exception is raised.
B.The vector grows in size to a capacity of 10 elements
C.The vector grows in size to a capacity of 15 elements
D.Nothing, the vector will have grown when the fifth element was added.
What should be the answer for the above. One of the Mock Exam says that it should be C. But I feel it should be B. Since JLS says that 2nd argument is the "capacityIncrement - the amount by which the capacity is increased when the vector overflows."
Thanks & Regards.

Anonymous
Ranch Hand
Posts: 18944
• Number of slices to send:
Optional 'thank-you' note:
import java.util.*;
public class Class1 {
public static void main(String args[]) {
Vector v = new Vector(5, 10);
System.out.println(v.capacity());
System.out.println(v.capacity());
}
}
Output :
5
15
Vector v = new Vector(5, 10); indicates -
Create a vector with initial capacity of 5
When it become full increase the capacity by 10 i.e 5 + 10.
Let me know if I'm wrong.

 If you're gonna buy things, buy this thing and I get a fat kickback: Free, earth friendly heat - from the CodeRanch trailboss https://www.kickstarter.com/projects/paulwheaton/free-heat