[RESOLVED] Return multiple values
Hello,
I just read that it is impossible to alter a parameter passed to a function. But I need a function to return 2 values. An integer and a string. Since I cannot use a parameter does anyone have a solution to return 2 values in a recursive function?
Kind regards
Re: Return multiple values
Use a separate class to encapsulate the two values and this class will be the return type of the method.
e.g.,
Code:
public class Foo
{
int i;
String s;
}
your method:
Code:
public Foo returnsFoo()
{
... return a new instance of the Foo class
}
If you need to alter alread-existing values, then just use a void method with a parameter of type 'Foo'.
Code:
public void AltersFoo(Foo f)
{
f.i = 2;
f.s = "new string";
}
This is perfectly valid since Java still allows you to modify the object via its public interface - you just can't reset the top reference 'f'.
Re: Return multiple values
Thanks man, I have it working!