In PHP, I can change the index and value of arrays.
$array = array("foo" => 54);
So then
$array["foo"]
returns 54. How can I do this in Java?
In PHP, I can change the index and value of arrays.
$array = array("foo" => 54);
So then
$array["foo"]
returns 54. How can I do this in Java?
The equivalent of PHP's associative array is a Map
in Java. Both share the key-value pair implementation with the most commonly used implementation being HashMap
Map<String, Integer> map = new HashMap<>();
map.put("foo", 54);
System.out.println(map.get("foo")); // displays 54
Other implementations exist such as LinkedHashMap
which preserves insertion order and TreeMap
which is sorted according to the natural ordering of its keys.