Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.*;
public class Class1 {
public static void main(String args[]) {
Vector v = new Vector(5, 10);
v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("5");
System.out.println(v.capacity());
v.add("6");
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic