A
Anand Gopinath
Hi,
I've got a question regarding Vectors and how it behaves when passed
around in functions.
Say I have a function foo that takes a vector v, and I pass off v to
another function to have stuff filled into it.
public int [] foo(Vector v )
{
...
return bar(v);
}
public int[] bar(Vector w )
{
...
w.add("something");
w.add("something else" );
return new int[] {1, 1};
}
The int[] is also needed by the caller of foo, so I can't return the
vector from bar after filling it up.
If foo is called from say the main function, will I get the things I
added into the vector from bar?
public static void main( String[] args )
{
Vector test = new Vector();
foo(test);
System.out.println(test.get(0) ); // should print out something
System.out.println(test.get(1) ); // should print out something else
}
I've tried it and the Vector test is empty when I get it back from foo.
Any suggestions would greatly help. Its not empty in foo if I changed it
to look like
public int [] foo(Vector v )
{
...
bar(v);
System.out.println( v.get(0) );
...
return new int[] {0,0};
}
Thanks,
Anand Gopinath
I've got a question regarding Vectors and how it behaves when passed
around in functions.
Say I have a function foo that takes a vector v, and I pass off v to
another function to have stuff filled into it.
public int [] foo(Vector v )
{
...
return bar(v);
}
public int[] bar(Vector w )
{
...
w.add("something");
w.add("something else" );
return new int[] {1, 1};
}
The int[] is also needed by the caller of foo, so I can't return the
vector from bar after filling it up.
If foo is called from say the main function, will I get the things I
added into the vector from bar?
public static void main( String[] args )
{
Vector test = new Vector();
foo(test);
System.out.println(test.get(0) ); // should print out something
System.out.println(test.get(1) ); // should print out something else
}
I've tried it and the Vector test is empty when I get it back from foo.
Any suggestions would greatly help. Its not empty in foo if I changed it
to look like
public int [] foo(Vector v )
{
...
bar(v);
System.out.println( v.get(0) );
...
return new int[] {0,0};
}
Thanks,
Anand Gopinath