2016-05-24 56 views
0

我試圖計算每個「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) 
           ^
+0

'ds(「Monday」,「h100」,20).productIterator.toList,'你爲什麼要這麼做,只能通過索引訪問元素?這根本沒有意義。 –

回答

1

之前回答你的問題,我想我應該談的差異列表元組

列表必須持有同樣之間類型的值,而元組可以包含不同類型的元素。出於這個原因,

​​

將產生的

List(Tuesday, h200, 30) 

第一元素是星期二一個列表,第二個是H200第三是。但是這些元素的類型是什麼?正如我們所知,列表必須包含相同類型的元素,顯然列表(星期二,H200,30)的類型字符串,並且也不詮釋

類型字符串詮釋是從類型的任何延長。因此,我們可以說,列表(星期二,H200,30)持有型的元素的任何

的「+ =」的bufInt需要運營商,類型詮釋bufString需求字符串,它們都不是任何。所以錯誤出現了。

只需修改這樣的代碼如下

case class ds(DayOfWeek : String, id: String, ViewTime: Long) 

val datasample = Array(ds("Monday", "h100", 20), ds("Tuesday", "h200", 30), ds("Monday", "h400", 10), ds("Sunday", "h100", 4), ds("Tuesday", "h300", 5), ds("Sunday", "h200", 0), ds("Tuesday", "h100", 1), ds("Tuesday", "h400", 50)) 

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) 

var j = 0 
var s = 0 

while (j <= y-1) { 
    x = datasample(j) 
    if (x.DayOfWeek == "Monday") { 
    bufInt += x.id.toInt 
    bufString += x.id 
    s += 1 
    } 
    j += 1 
} 
println(bufInt) 
println(bufString) 
} 

好運

0

首先,你要什麼做的稍微少扭曲的方式:

case class ds(DayOfWeek : String, id: String, ViewTime: Long) 

val datasample = Array(ds("Monday", "h100", 20), 
         ds("Tuesday", "h200", 30), 
         ds("Monday", "h400", 10), 
         ds("Sunday", "h100", 4), 
         ds("Tuesday", "h300", 5), 
         ds("Sunday", "h200", 0), 
         ds("Tuesday", "h100", 1), 
         ds("Tuesday", "h400", 50)) 

// changed Int to Long here to match the case class 

val bufInt = scala.collection.mutable.ListBuffer.empty[Long] 
val bufString = scala.collection.mutable.ListBuffer.empty[String] 

for (sample <- datasample 
    if sample.DayOfWeek == "Monday") { 
     bufInt += sample.ViewTime 
     bufString += sample.id 
} 
println(bufInt)         //> ListBuffer(20, 10) 
println(bufString)        //> ListBuffer(h100, h400) 

,然後更習慣的方法:

datasample.collect{case ds(day, id, _) if day == "Monday" => id} 
//> res0: Array[String] = Array(h100, h400) 
datasample.collect{case ds(day, _, time) if day == "Monday" => time} 
//> res1: Array[Long] = Array(20, 10) 

或者如果你喜歡for內涵:

for (sample <- datasample 
    if sample.DayOfWeek == "Monday") yield sample.ViewTime 
               //> res2: Array[Long] = Array(20, 10) 

for (sample <- datasample 
    if sample.DayOfWeek == "Monday") yield sample.id 
               //> res3: Array[String] = Array(h100, h400) 

case類等的全部意義在於提供收集的東西連成一個結構的一種便捷方式。將它們與productIterator打包到一個列表中只是奇怪的,我不知道爲什麼你開始沿着這條路線。

相關問題