HashMap(key: String, value: ArrayList) returns an Object instead of ArrayList?

前端 未结 5 1830
别那么骄傲
别那么骄傲 2021-02-04 17:11

I\'m storing data in a HashMap with (key: String, value: ArrayList). The part I\'m having trouble with declares a new ArrayList \"current,\" searches the HashMap for the String

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-04 17:45

    public static void main(String arg[])
    {
        HashMap> hashmap = 
            new HashMap>();
        ArrayList arraylist = new ArrayList();
        arraylist.add("Hello");
        arraylist.add("World.");
        hashmap.put("my key", arraylist);
        arraylist = hashmap.get("not inserted");
        System.out.println(arraylist);
        arraylist = hashmap.get("my key");
        System.out.println(arraylist);
    }
    
    null
    [Hello, World.]
    

    Works fine... maybe you find your mistake in my code.

提交回复
热议问题