Preamble: I am aware of using a list or other collections to return a result but then I have to go through the list plucking the results out: see 2nd example
Preamble-2: I'm looking for an answer beyond "this is not supported in Java ..."
I'm looking for a convenient way to return multiple objects from a Java method call.
Kind of like in PHP:
list ($obj1, $obj2, ...) foobar();
I'm really getting tired of passing holder objects in the arguments for example:
class Holder {
int value;
}
Holder h1=new Holder();
Holder h2=new Holder();
and then:
o.foobar(h1,h2);
... would be very interested if someone has figured an elegant way to get round this.
Using a list
List<String> = foobar();
There are two drawbacks to this:
I have to first pack the List on the callee side of the house:
// this is on the callee side of the house
ArrayList<String> result = new ArrayList<String>
result.add("foo");
result.add("bar");
Then on the caller side I have to pluck the results out:
// This is on the caller side
List<String> result = foobar();
String result1 = result.get(0);
String result2 = result.get(1); // this is not as elegant as the PHP equivalent
Further, say I wanted to return objects of different types say String, Integer I would have to return a list of Objects and then cast each object ... not pretty
Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…