Stefan Muellerheim wrote:
why is it necessary to init the capacity, when it is not usable? where is my fault?
It is not necessary to init the capacity -- the vector will grow as needed as you add more elements. The reason that you may want to init the capacity is if you know how many elements you will be using, and you don't want the vector wasting time growing the capacity for you.
The capacity is just the amount of memory reserved for you. It is not the number of elements in the vector. And it is not possible to make an element the 5th element when there aren't four elements to put it after.
Remember that you're setting the capacity not the size. The initial size is 0 and the capacity is 20. So when you're trying to insert a new element at a higher then 0 you'll get an Exception.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Joined: Sep 17, 2010
thanks, now it's easy to understand.
a Vector is only at a specified position settable if there is an object already.
a Vector have to set from the beginning always.
if any position, then it has to filled up with 'null'.
if any position, size() is not usable, due to the dummy-objects.
Vector should make it possible to set an(y) object to any position (without adding 'null').