简体   繁体   中英

Doing math operations on Scala objects in Java code

In my java code I am using a method, let's call foo() , that returns a scala.Double[] .

scala.Double[] arr = foo();

I want to sum up arr into a scala.Double , but am having trouble. I've found that doing any kind of math operation on scala.Double s gets me an error message from my IDE:

arr[0] + arr[1];
arr[0] * arr[1];
//etc

get me messages like

Operator '+' cannot be applied to 'scala.Double', 'scala.Double'

How can I do math operations on scala.Double s within java code? (alternatively, is there another way to sum up a scala.Double[] within java code?)

It's not clear how you got scala.Double[] in Java. If you have Array[Double] in Scala, it's seen in Java as double[] , not scala.Double[] .

Anyway, if you have scala.Double[] in Java

public class App {
  public static scala.Double[] arr = foo();
}

then it's seen in Scala also as Array[scala.Double]

object App1 {
  val arr1: Array[Double] = App.arr
}

and then the latter is seen back in Java as double[]

public class App {
  public static scala.Double[] arr = foo();
  public static double[] arr2 = App1.arr1();
}

Then you can do arr2[0] + arr2[1] , arr2[0] * arr2[1] ...

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