Associative Array

Aus My Wiki

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 info = new HashMap(); // 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>