rodi rodi - 3 months ago 10
Java Question

What is the equivalent of PHP's list() function in Java?

What is the equivalent of PHP's

list()
function in Java? For example

$matches = array('12', 'watt');
list($value, $unit) = $matches;

Answer

Due to java always passes primitives by value and does not have native support of perl style lists I am afraid there is no way to do what you need.

You can write method list and pass there variables value and unit. You can changes values of these variables. But the changes will be visible into the list method only. The original values of value and unit will be the same as before the call.

The java-style solution for this problem is creating custom class (even inner class it does not have any sense in other contexts). Then create method that parses your string (using regex) and creates instance of the class.

Comments