• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

vector question HUNT

 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
}
}
 
Acetylsalicylic acid is aspirin. This could be handy too:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic