ArrayList到Hashmap转换

时间:2014-05-20 15:45:22

标签: java arraylist hashmap

这最初是一个arraylist,我转换成了一个hashmap但是我得到了错误 在方法添加形状从开关(类型),我将shapes.add更改为shapes.put然而仍然有错误,感谢帮助谢谢。

    switch (type) { // getting errors here on all cases 
        case "Polygon":
            shapes.put(new RegularPolygon(name, val1, val2, x, y));
            break;
        case "Rectangle":
            shapes.put(new Rectangle(name, val1, val2, x, y));
            break;
        case "CharL":
            shapes.put(new LCharacter(name, val1, val2, x, y));
            break;
        default:
            errorMessage = "Invalid type " + type;
            break;

1 个答案:

答案 0 :(得分:2)

Map.put()函数接受两个参数:首先是键,然后是值。你只提供价值。

您的代码应如下所示:

shapes.put("someKey", someShape);

有关详细信息,请参阅API:http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html