1
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.!

2
  • 3
    those variable names are painfully long Commented Jun 10, 2013 at 11:26
  • Sorry but I find the question difficult to understand. Could you please give some hint to the type of questionSetQuestionQuestion? Commented Jun 10, 2013 at 12:30

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.