• Post Reply Bookmark Topic Watch Topic
  • New Topic

vector question HUNT  RSS feed

 
sarim raza
Ranch Hand
Posts: 232
  • 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
//I REALLY DONT KNOW WHAT IS GOING ON HERE
 
Sahir Shah
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sarim,
First parameter is initial capacity the second one is the capacity increment value. This denotes by how many elements the capacity is increased when the vector overflows. The answer is obvious now. Isnt it?
Rgds
Sahir
[This message has been edited by Sahir Shah (edited December 21, 2000).]
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is asking about the constructor for the Vector class. Easiest thing to do then is look it up in the API.
The constructor says:

Vector (int initialCapacity, int capacityIncrement)

So you know it starts with 5 and will increase by 10. Now the next question is when does it increase to 10, when you add the 5th element or the 6th element. Again look at the API:

capacityIncrement - the amount by which the capacity is increased when the vector overflows.

So it will increase when it overflows, which makes sense from a efficiency standpoint. That means when the 6th element is added, it will increase 10 more elements making C the correct answer as it will now have 15 elements.
Bill
 
anil bisht
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this code and see
import java.util.Vector;
class A {
public static void main(String args[])
{
int i,j;
Vector v = new Vector(5,10);
for (int l = 1;l<8 ;++l ){
v.addElement(new Integer(5));
i = v.size();
j = v.capacity();
System.out.println("element no " +l +" size= " + i+ " capacity = "+j );}
//c = b
}
}
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!