• 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

Java Vector simple Q

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Stefan Müllerheim
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic