简体   繁体   中英

Loop over listbuffer scala

I'm new in scala language I have a listbuffer :

    var oldQuestions: Seq[Question] = section.questions
    var newQuestions: ListBuffer[Question] = new ListBuffer()

So all I need is,to loop over the newQuestions list and access to one question based on her id and delete it. Any help would be very appreciated.

Don't use vars, and mutable collections. It is a really, really rare occurrence that you actually need either of those in scala. So, for now, until you get sufficient grip on the language to be able to tell when those rare cases happen, just pretend these things don't exist. Learn to write good functional code before you explore mutability.

To answer your question:

  val newQuestions: Seq[Question] = section.questions.filterNot(_.id == idToDelete)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM