Associative array/Creation: Difference between revisions
Content added Content deleted
(→{{header|Vlang}}: Rename "Vlang" in "V (Vlang)") |
(Add Ecstasy example) |
||
Line 2,769: | Line 2,769: | ||
→ 3 |
→ 3 |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Ecstasy}}== |
|||
Defining the Map: |
|||
<syntaxhighlight lang="java">Map<String, Int> map = new HashMap(); |
|||
map["foo"] = 5; // or: map.put("foo", 5) |
|||
map["bar"] = 10; |
|||
map["baz"] = 15; |
|||
map["foo"] = 6; // replaces previous value of 5</syntaxhighlight> |
|||
Literal map notation: |
|||
<syntaxhighlight lang="java">Map<String, Int> map = ["foo"=6, "bar"=10, "baz"=15];</syntaxhighlight> |
|||
Retrieving a value: |
|||
<syntaxhighlight lang="java">Int? mightBeNull = map["foo"]; |
|||
Int neverNull = map.getOrDefault("foo", 0); |
|||
if (Int n := map.get("foo")) |
|||
{ |
|||
// if "foo" is in the map, then the variable "n" is set to its value |
|||
} |
|||
else |
|||
{ |
|||
// if "foo" is not in the map, then the variable "n" is not defined |
|||
}</syntaxhighlight> |
|||
Iterate over keys: |
|||
<syntaxhighlight lang="java">for (String key : map) |
|||
{ |
|||
// the variable "key" is defined here |
|||
}</syntaxhighlight> |
|||
Iterate over values: |
|||
<syntaxhighlight lang="java">for (Int value : map.values) |
|||
{ |
|||
// the variable "value" is defined here |
|||
}</syntaxhighlight> |
|||
Iterate over key, value pairs: |
|||
<syntaxhighlight lang="java">for ((String s, Int i) : map) |
|||
{ |
|||
// the variables "key" and "value" are defined here |
|||
}</syntaxhighlight> |
|||
=={{header|Elena}}== |
=={{header|Elena}}== |