|
-
Oct 4th, 2009, 04:53 PM
#1
Thread Starter
Fanatic Member
[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
Don't Hate Me Cause You Ain't Me
-
Oct 4th, 2009, 06:48 PM
#2
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'.
Last edited by David Anton; Oct 4th, 2009 at 07:02 PM.
-
Oct 4th, 2009, 07:49 PM
#3
Thread Starter
Fanatic Member
Re: Return multiple values
Thanks man, I have it working!
Don't Hate Me Cause You Ain't Me
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|