简体   繁体   English

Scala从Case类遍历Double数组

[英]Scala iterate over Double array from Case class

EDIT / UPDATE: PROBLEM IS FIXED! 编辑/更新:问题已解决!

This is kinda strange as iterating over collections in Scala is usually straight forward but I do get a compile error while using an double array from a case class. 这有点奇怪,因为在Scala中遍历集合通常很简单,但是在使用case类中的double数组时,确实出现了编译错误。 The error says: 错误提示:

 error: value foreach is not a member of Array[Double] for(d <- data.data_arr) 

okay, here is the case class: 好的,这是案例类:

case class StatsData (name: String,
                  timeUnit: TimeUnit,
                  data_arr: Array[Double],
                  min: Double,
                  max: Double){}

And here is the critical point: 这是关键点:

  /*Doesn't work */ 
 for(d <- data.data_arr) {         
  println(d) // can't fetch value d here   
  number = new Number(col, row, d)
 }

Strange thing is, no matter what kind of iteration I'm using, it simply doesn't work. 奇怪的是,无论我使用哪种迭代,它都根本不起作用。 For example using array index 例如使用数组索引

 for (i <- data_arr.length-1)

as well as converting the array to a sequence throws exactly the same error as above; 将数组转换为序列会引发与上述完全相同的错误;

for(d <- data.data_arr.toSeq)

What am I doing wrong? 我究竟做错了什么?

Thanks for any help on this matter. 感谢您对此事的任何帮助。

EDIT / UPDATE: PROBLEM IS FIXED! 编辑/更新:问题已解决!

As it turns out, the cause of the problem was an issue within IntelliJ's Project settings or structure, I cannot say it for certain but it was all solved by creating a new project. 事实证明,问题的原因是IntelliJ的Project设置或结构中的一个问题,我不能确定地说出来,但是通过创建一个新项目可以解决所有问题。 The same code runs now perfectly fine. 现在,相同的代码可以完美运行。 Sorry for that but nearly all of the post has helped me to trace the issue down. 抱歉,但是几乎所有帖子都帮助我找出了问题所在。

@yakshaver @yakshaver

What is the "yield" expression exactly used for? 准确使用的“收益”表达式是什么?

Thanks for all the help. 感谢您的所有帮助。

Both

Array(1, 2, 3) foreach println

and

for(i <- Array(1, 2, 3)) yield i

and

for(i <- Array(1, 2, 3)) { println(i) }

work nicely in 2.10.0-RC3 for me. 对我来说,在2.10.0-RC3中效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM