我有一个List
case 对象,其中一些值为NaN
s,我必须将它们替换为0.0
. 到目前为止,我尝试了这段代码:
var systemInformation: List[SystemInformation] = (x.getIndividualSystemInformation)
systemInformation.foreach[SystemInformation] {
_ match {
case SystemInformation(a, b, c, d, e, f, g, h, i, j, k, l, m, x) if x.isNaN()
=> SystemInformation(a, b, c, d, e, f, g, h, i, j, k, l, m, 0.0)
case SystemInformation(a, b, c, d, e, f, g, h, i, j, k, l, m, x) if !x.isNaN()
=> SystemInformation(a, b, c, d, e, f, g, h, i, j, k, l, m, x)
}
}
但这不会将更改写回systemInformation
. 所以我添加了另一个 List 但类型不匹配:
var systemInformation: List[SystemInformation] = (x.getIndividualSystemInformation)
var systemInformationWithoutNans: ListBuffer[SystemInformation] = new ListBuffer[SystemInformation]
systemInformation.foreach[SystemInformation] {
_ match {
case SystemInformation(a, b, c, d, e, f, g, h, i, j, k, l, m, x) if x.isNaN()
=> systemInformationWithoutNans += SystemInformation(a, b, c, d, e, f, g, h, i, j, k, l, m, 0.0)
case SystemInformation(a, b, c, d, e, f, g, h, i, j, k, l, m, x) if !x.isNaN()
=> SystemInformation(a, b, c, d, e, f, g, h, i, j, k, l, m, x)
}
}
错误发生在与+=
和 的行上,如下所示:
type mismatch;
found : scala.collection.mutable.ListBuffer[com.x.interfaces.SystemInformation]
required: com.x.interfaces.SystemInformation
为什么这不起作用?NaN
用 s 替换s的更好方法是0.0
什么?