• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

quick question about vectors

 
Malcolm Whitely
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone,
I'd like to use vectors in my program instead of arrays because of the advantages of them being dynamic. Here is my question:
If I originally had an array of type "typeX" declared as such:
typeX[] array = new typeX[10];
How could I declare a vector of type typeX? I've tried:
typeX Vector myVector = new Vector();
but that doesn't seem to work.
Any help would be appreciated.
Thanks
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couple of things:
1. Use ArrayList instead. Vector basically shouldn't be used anymore.
2. Currently you can't declare an ArrayList to be of a certain type. You can add any Object to an ArrayList, but the drawback is that you usually have to cast them when you retrieve them in order for them to be useful. For example:

There is some kind of an initiative (or JRS or something like that) to add that functionality to the language. Check out this link on generics
 
boyet silverio
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello Malcolm,
Blake's right there.
On your vector query, a vector object is of type Vector. So your 'typeX Vector myVector = new Vector()' will not work as the objects references contained in the vector object could be of different types. 'Vector myVector = new Vector();' will do.
[ February 25, 2003: Message edited by: boyet silverio ]
[ February 25, 2003: Message edited by: boyet silverio ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic