It's not a secret anymore!
The moose likes Beginning Java and the fly likes Retrieving Vector Elements?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Retrieving Vector Elements??" Watch "Retrieving Vector Elements??" New topic

Retrieving Vector Elements??

Rik Ny

Joined: Oct 13, 2001
Posts: 13
I have created a Vector which stores random numbers, but I am having trouble retrieving them. How do I retrieve each individual element and print it to a screen??
P.S This vector stores numbers (int)
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
You want to use either get(index) or elementAt(index). Both of them take as an arguemnet the index of the element you want to get. To get all of the elements from a Vector this would do it for you:

This will just use what the default toString() implementation is for whatever object is stored in the Vector. To do something useful just cast it to whatever type of Object it is supposed to be and manipulate it then.
hope that helps

Dorothy Finkel-Laverty
Ranch Hand

Joined: Nov 24, 2001
Posts: 51
You can't store ints in a Vector. You can only store Objects. That might be contributing to your problem.....
Also, I would investigate the elements() method in the Vector class. It returns an Enumeration, and you can step through the list nicely. Check out the API for Vector and for Enumeration for the methods.
I agree. Here's the link:
subject: Retrieving Vector Elements??
It's not a secret anymore!