Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

php

list

split

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

$matches = array('12', 'watt');
list($value, $unit) = $matches;
like image 337
M4rk Avatar asked Feb 25 '23 06:02

M4rk


1 Answers

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.

like image 88
AlexR Avatar answered Feb 26 '23 20:02

AlexR