这段代码要求做什么?

时间:2017-07-06 21:47:58

标签: scala

case class TempData(year:Int,month:Int,Prec:Double,Maxtemp:Int,Meantemp:Int,Mintemp:Int)

def parseLine(line:String):TempData = {
  val p = line.split(",")
  TempData(p(1).toInt,p(4).toInt,p(5).toDouble,p(6).toInt,p(7).toInt,p(8).toInt)  
}                        

1 个答案:

答案 0 :(得分:1)

它只需要一行(CSV)将其按,拆分,然后根据这些值创建TempData个实例。

参见示例,

scala> case class TempData(year:Int,month:Int,Prec:Double,Maxtemp:Int,Meantemp:Int,Mintemp:Int)
defined class TempData

scala> val p = "whatever,2017,whatever,whatever,10,5.0,1000,2000,3000".split(",")
p: Array[String] = Array(whatever, 2017, whatever, whatever, 10, 5.0, 1000, 2000, 3000)

scala> TempData(p(1).toInt,p(4).toInt,p(5).toDouble,p(6).toInt,p(7).toInt,p(8).toInt)
res1: TempData = TempData(2017,10,5.0,1000,2000,3000)

相当于

scala> TempData(year=p(1).toInt, month=p(4).toInt, Prec=p(5).toDouble, Maxtemp=p(6).toInt, Meantemp=p(7).toInt, Mintemp=p(8).toInt)
res2: TempData = TempData(2017,10,5.0,1000,2000,3000)

在scala中了解String#splitcase classes