Assigning Values to an Array: Difference between revisions

→‎Java: Code already added to Arrays
m (→‎Ruby: move to Arrays)
(→‎Java: Code already added to Arrays)
Line 303:
88 99 88 (2 3 4}) array NB. Multiple update
5 5 88 99 88 5 5
 
==[[Java]]==
{{works with|J2SE| 1.2+}}
<lang java>public void writeToIntArray(int[] array, int loc, int val){
array[loc]=val;
}</lang>
{{works with|Java|1.5+}}
Things that Java people call "arrays" generally don't use the "key/value" vocabulary. In Java, there is an object called a Map (what other languages might call a "dictionary") with keys and values that works for this task. Maps are sorted by some aspect of the key in Java (hash, address, or natural order). The only thing we need to change is an error for a non-existent key.
<lang java>import java.util.TreeMap;
public void replaceInMap(TreeMap<Integer, Integer> map, int key, int val){
if(!map.containsKey(key)){
Systen.err.println("Key does not exist");
return;
}
map.put(key,val);
}</lang>
 
==[[mIRC Scripting Language]]==
Anonymous user