【发布时间】:2012-02-28 00:14:58
【问题描述】:
这是我的代码:
HashMap<String, String> inst1 = new HashMap(instructorIO.getInstructors());
instListModel = new DefaultListModel<String>(inst1.values());
我收到错误:
DepartmentProject.java:69: error: constructor DefaultListModel in class DefaultListModel<E> cannot be applied to given types;
required: no arguments
found: Collection<String>
reason: actual and formal argument lists differ in length
where E is a type-variable:
E extends Object declared in class DefaultListModel
谁能帮我解决这个问题?
【问题讨论】:
-
DefaultListModel不带任何参数,您将其传递给一个集合。 -
有没有办法将 hashmap 中的每个值放入列表模型中?
-
您需要将元素添加到
ListModel,因此遍历您的HashMap并将每个值添加到ListModel -
当我对 (a.getValue()) 进行循环并尝试将它们添加到我得到 DepartmentProject.java:72: 错误:类 DefaultListModel
中的方法 addElement 不能应用于给定类型;实参Object不能通过方法调用转换为String,其中E是类型变量: -
我开始工作了,我把它改成了 (a.getValue() + " ");不知何故它通过了
标签: java hashmap defaultlistmodel