Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

quick question about vectors  RSS feed

 
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
 
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
 
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!