我試圖計算每個「DayOfWeek」的「ViewTime」和每個「id」的每個「DayOfWeek」的總和和平均值。作爲一個例子,當DayOfWeek ==「Monday」時,我試圖做到這一點。我試圖製作一個id列表和一個ViewTime列表,但遇到以下錯誤。什麼是最好的解決方案?如何將列表元素的值作爲Scala中緩衝區列表的新元素?
case class ds(DayOfWeek : String, id: String, ViewTime: Long)
val datasample = Array(ds("Monday", "h100", 20).productIterator.toList, ds("Tuesday", "h200", 30).productIterator.toList, ds("Monday", "h400", 10).productIterator.toList, ds("Sunday", "h100", 4).productIterator.toList, ds("Tuesday", "h300", 5).productIterator.toList, ds("Sunday", "h200", 0).productIterator.toList, ds("Tuesday", "h100", 1).productIterator.toList, ds("Tuesday", "h400", 50).productIterator.toList)
var bufInt = scala.collection.mutable.ListBuffer.empty[Int]
var bufString = scala.collection.mutable.ListBuffer.empty[String]
val y = datasample.length
var x = ds("Tuesday", "h200", 30).productIterator.toList
var j = 0
var s = 0
while (j <= y-1) {
x = datasample(j)
if (x(0) == "Monday") {
bufInt += x(2)
bufString += x(1)
s += 1
}
j += 1
}
println(bufInt)
println(bufString)
錯誤:
<console>:61: error: type mismatch;
found : Any
required: Int
bufInt += x(2)
^
<console>:62: error: type mismatch;
found : Any
required: String
bufString += x(1)
^
'ds(「Monday」,「h100」,20).productIterator.toList,'你爲什麼要這麼做,只能通過索引訪問元素?這根本沒有意義。 –