aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Retrieving Vector Elements??" Watch "Retrieving Vector Elements??" New topic
Author

Retrieving Vector Elements??

Rik Ny
Greenhorn

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)
Cheers,Rik
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Rik
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


Dave
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: http://aspose.com/file-tools
 
subject: Retrieving Vector Elements??
 
Similar Threads
Search Algorithm
objects and Vectors
NX: Design review
"has a"
public vs private instance variables