import java.util.HashMap;
public class CollectionHashMap {
public static void main(String[] args) {
HashMap <String, String> hmap = new HashMap<&gr;();
hmap.put("1號","鳳梨");
hmap.put("2號","草莓");
hmap.put("3號","西瓜");
System.out.println("水果籃子有: "+ hmap);
if(hmap.containsKey("3號"))
System.out.println("重複購買水果:(\"3號\",\"草莓\")");
hmap.put("3號","草莓");
System.out.println("水果籃子有: "+ hmap);
System.out.println("增購水果:(\"4號\",\"西瓜\")");
hmap.put("4號","西瓜");
System.out.println("水果籃子有: "+ hmap);
System.out.println("取消購買水果:(\"4號\",\"西瓜\")");
hmap.remove("4號");
System.out.println("水果籃子有: "+ hmap);
}
}
執行結果
沒有留言:
張貼留言