Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes quick question about vectors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "quick question about vectors" Watch "quick question about vectors" New topic

quick question about vectors

Malcolm Whitely

Joined: Feb 06, 2003
Posts: 16
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.
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
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

Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
boyet silverio
Ranch Hand

Joined: Aug 28, 2002
Posts: 173
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 ]
I agree. Here's the link:
subject: quick question about vectors
It's not a secret anymore!