• Post Reply Bookmark Topic Watch Topic
  • New Topic

returing a vector from a method

 
kalyani anne
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am writing a javabean in which i have a vector object.to that i am adding elements by
using addElement() method.
now in another method i want to show that values..like i have to return the values in that method.
how can i return the vector values...
can anyone help me
thanks in advance
 
Larry Anderson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Kalyani,
What I've always done is VectorName.elementAt(index), where index is the number of the object that you want, and then making sure that you casted the object to the type that you wanted.
Here's an example:

Date alreadyInitializedDate = new Date();
Date retrievedDate;
Vector sample = new Vector(7,7);
sample.add(alreadyInitializedDate);
retrievedDate = (Date)sample.elementAt(0);

I hope it helps,
Larry
 
Stanley Tan
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could return an Enumeration of the vector items by calling vector.elements();
Your method would look like:
public Enumeration someMethod() {
    return myVector.elements();
}
Then to access the elements just loop through the Enumeration
Enumeration e = whatever.someMethod();
while (e.hasMoreElements()) {
SomeObject obj = (SomeObject) e.nextElement();
//do something with obj here
}
HTH
 
nagendra barla
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey kalyani..
u declare one global vector object
and write get and set methods.
Vector sampleVector;
public void setVector(Vector sampleVector){
this.sampleVector=u'r vector containing ----;
}
public Vector getVector(){
return sampleVector;
}
when ever u want u just
Vector someX=getVector();
and use someX.elementAt(0);

u can get it
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!