Reputation: 614
var questionList:List[Question]=null
questionSetQuestionQuestion=questionSetQuestionService.findQuestionSetQuestionByQuestionSetId(id)
// println(questionSetQuestionQuestion(0))
for(x<-questionSetQuestionQuestion){
questionList=x.getQuestion()
}
Here I want to add each getQuestion() values to questionList.!
Upvotes: 1
Views: 81
Reputation: 35443
Within the Scala world, mutability should be avoided if possible, so while looping and prepending to a List var will work, there are more palatable options. In your case, I would think of it more as "How can I build a new Collection from an existing one I already have?". Taking that approach you could try:
val newColl = for(x<-questionSetQuestionQuestion)
yield x.getQuestion()
Or you could just use the map
function directly like this:
val newColl = questionSetQuestionQuestion.map(_.getQuestion)
Upvotes: 3
Reputation: 31724
You need to define it as an empty List and then append to it
var questionList:List[Question]=List[Question]()
questionSetQuestionQuestion=questionSetQuestionService.findQuestionSetQuestionByQuestionSetId(id)
for(x<-questionSetQuestionQuestion){
questionList=x.getQuestion() :: questionList
}
PS: I would recommend lectures by Martin Odersky on Coursera (course: Functional Programming in Scala) where he explains general usage of most of the data structures
Upvotes: 0