Search code examples
arraysscalalistfor-loopupdating

Update List with Loop Arguments Scala


I'm trying create new random List based on other List. But this code doesn't work to update the var

import scala.util.Random
import scala.math

val randSymbol = List(1,2,2,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,6,6,7,7,7,7,7,7,8)
val _finalSymbol = List(List(0,0,0,0,0),List(0,0,0,0,0),List(0,0,0,0,0))
    var i:Int = 0
    var a:Int = 0
    for (i <- 0 to 2){
      _finalSymbol(i) = new List
      for (a <- 0 to 4){
        var iRandIndex = floor(Random.nextInt() * randSymbol.length).toInt
        var iRandSymbol = randSymbol(iRandIndex)
        _finalSymbol(i)(a) = iRandSymbol
      }
    }

Solution

  • Try something like this:

    val _finalSymbol = (0 to 2) map { _ => Random.shuffle(randSymbol).take(5) }
    

    And do yourself a favor: buy a scala book. It's not javascript. At all.