aspose file tools*
The moose likes Beginning Java and the fly likes Java Vector simple Q Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Vector simple Q" Watch "Java Vector simple Q" New topic
Author

Java Vector simple Q

Stefan Müllerheim
Greenhorn

Joined: Sep 17, 2010
Posts: 12
hi an easy question,

if a Vector is new to built , first the capacity size is to init.



now, if i try to set a position with an object i am going to get an "out of range" error.



the solution is simple: in a loop with my capacity size, every position have first to set with "null".

but that makes no sin for me.

why is it necessary to init the capacity, when it is not usable? where is my fault?


less is more.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18971
    
  40

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.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.
Stefan Müllerheim
Greenhorn

Joined: Sep 17, 2010
Posts: 12
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').
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Are you sure that you want a Vector and not an ArrayList? They are the same but the methods of the Vector are synchronized. You could also use a Map with an Integer key.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19757
    
  20

Stefan Muellerheim wrote:Vector should make it possible to set an(y) object to any position (without adding 'null').

Then what should it do with the indexes in between? What should the elements at indexes 0 to 4 be?

An easy way is to simply create a utility method:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Vector simple Q