Associative Array: Unterschied zwischen den Versionen
Aus My Wiki
Keine Bearbeitungszusammenfassung |
Keine Bearbeitungszusammenfassung |
||
| Zeile 6: | Zeile 6: | ||
public class PhoneDirectory { | public class PhoneDirectory { | ||
private HashMap | private HashMap<String,String> links = new HashMap<String,String>(); // Stores the data for | ||
// the phone directory. | // the phone directory. | ||
Aktuelle Version vom 7. Mai 2009, 08:40 Uhr
Ist über java.util.HashMap lösbar, da Java assoziative Arrays nicht als primitive Datenstruktur unterstützt ([1]).
<source lang=java>
import java.util.HashMap;
public class PhoneDirectory {
private HashMap<String,String> links = new HashMap<String,String>(); // Stores the data for
// the phone directory.
public void addEntry(String name, String number) {
// Record the phone number for a specified name.
info.put(name,number);
}
public String getNumber(String name) {
// Retrieve the phone number for a specified name.
// Returns null if there is no number for the name.
return (String)info.get(name);
}
}
</source>