Saturday, 28 September 2013

Vector instantiation in java

Vector instantiation in java

I have a class Fruit in whose constructor I am instantiating a
vector(vector of different fruits). And In that class I am writing a
searchVector method. I want to invoke that method from another class
searchFruit but if I create an object of the Fruit class, a new vector
gets initialized. When I call the searchVector method, the vector is
empty(as new vector gets created) and hence the search fails. Is there a
way that I could call the searchVector method from searchFruit class.
class Fruit{
Vector v;
Fruit(){
v= new vector();
}
public Fruit searchVector(String fruit){
//Searches Fruit
}
}
class searchFruit{
Fruit apple = new Fruit();
apple.searchVector("Apple");
}
Or if I want to write the search function in the searchFruit class how do
I pass the created vector to that class.

No comments:

Post a Comment