Note that there are some explanatory texts on larger screens.

plurals
  1. POreturning multiple values in Java
    text
    copied!<p>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</p> <p>Preamble-2: I'm looking for an answer beyond "this is not supported in Java ..."</p> <hr> <p>I'm looking for a convenient way to return multiple objects from a Java method call.</p> <p>Kind of like in PHP:</p> <pre><code>list ($obj1, $obj2, ...) foobar(); </code></pre> <p>I'm really getting tired of passing holder objects in the arguments for example:</p> <pre><code>class Holder { int value; } Holder h1=new Holder(); Holder h2=new Holder(); </code></pre> <p>and then:</p> <pre><code>o.foobar(h1,h2); </code></pre> <p>... would be very interested if someone has figured an elegant way to get round this.</p> <hr> <p>Using a list</p> <pre><code>List&lt;String&gt; = foobar(); </code></pre> <p>There are two drawbacks to this:</p> <p>I have to first pack the List on the callee side of the house:</p> <pre><code>// this is on the callee side of the house ArrayList&lt;String&gt; result = new ArrayList&lt;String&gt; result.add("foo"); result.add("bar"); </code></pre> <p>Then on the caller side I have to pluck the results out:</p> <pre><code>// This is on the caller side List&lt;String&gt; result = foobar(); String result1 = result.get(0); String result2 = result.get(1); // this is not as elegant as the PHP equivalent </code></pre> <p>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</p> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload