简体   繁体   中英

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. 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. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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