?

Log in

No account? Create an account
penrose orange

stephenw32768


/var/log/stephen

cat /var/log/stephen >/dev/eyes


I want multiple return values
penrose orange
stephenw32768
Did the front end; went fairly smoothly. This morning, I simply realized how to do it, and it worked. Don't know what was clouding my thoughts yesterday; probably just a case of end-of-the-day brain rot.

I wish functions in Java could return multiple values. I'd like to be able to declare functions with signatures resembling:

public String, String foo()

and use them like this:

String a;
String b;

a, b = foo();

The closest that you can get is to return the two Strings in an array:

public String[] foo()

It works, but it's clumsy:

String[] temp;
String a;
String b;

temp = foo(); a = temp[0]; b = temp[1];

Which is all well and good, but what if you want to return two values of different types, such as a String and an int? The ideal signature would be:

public String, int foo()

But you can't do that. The closest you can do is return an array of generic Objects, and typecast the results when you retrieve them from the array:

Object[] temp;
int a;
String b;

temp = foo();
a = ((Integer)temp[0]).intValue();
b = (String)temp[1];

Blegh, how disgusting.