我正在尝试将值附加到scala类中的地图。
我的控制器类中有一个带有question / answerList对的通用映射:
static Map<Question, List<Answer>> myMap = new HashMap<Question, List<Answer>>();
Question question1 = new Question("xyz", "Do Androids dream?", 127, "Marcus");
Answer answer11 = new Answer("zab", "xyz", "Only of electric sheep!", 70, "Sarah");
List<Answer> answerList1 = new ArrayList<Answer>();
answerList1.add(answer11);
// puts the question and its answers into the hashmap
myMap.put(question1, answerList1);
public static Result askQuestion(){
return ok(views.html.questionAnswer.render(myMap));
}
在我的视图类中,我在地图中添加了一个新的问题/答案对:
@import model.Question
@import model.Answer
@(myMap: Map[Question, List[Answer]])
@main("Ask question"){
@myMap.put(new Question("id123", "questiontext", 34, "userID"), new List[Answer]);
}
但是当我运行代码时,出现错误页面,说:&#34; trait List是抽象的;无法实例化#34;。为什么我不能把(问题,列表[答案])对放入我的地图?
答案 0 :(得分:4)
您收到错误,因为正如您的错误消息所述:您无法实例化抽象类。
您需要实例化它的子类。
new List[Answer]
永远不会奏效。实例化一个适合您需求的子类。
答案 1 :(得分:0)
由于Stultuske和Kulu Limpa指出您需要实例化一个子类。这将有效:
// Note: You need to tell the compiler which type Nill should use
@myMap.put(new Question("id123", "questiontext", 34, "userID"), Nil: List[Answer]);