作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是 Scala 的新手,目前正在从事一个涉及 Java 和 Scala 模块的项目。现在我想使用 byte[] 类型的参数从 Java 调用 Scala 方法。
Scala 方法具有签名:def foo(data: Array[Byte])
Java 调用如下所示:foo(x)
,其中 x 的类型为 byte[]
。
IDE 告诉我这是不可能的:
The method foo(Array) in the type Bar is not applicable for the arguments (byte[])
作为附加约束,不建议更改 Scala 方法。在 Java 方面,我尝试使用 Byte[]
,但这并没有解决问题。一定存在某种转换?
最佳答案
正如其他人指出的那样,转换没有问题。我的 IDE 行为错误,并显示编译没有问题的假想错误。此时调用main-method中的receive方法在如下代码中被标记为错误:
The method receive(Array) from the type ScalaByteReceiver refers to the missing type Array
But this code, which exemplifies my question, compiles fine and yields the expected result:
Java:
package stackOverflow;
public class JavaByteSender {
public static void main(String... args) {
new ScalaByteReceiver().receive(new byte[4]);
}
}
斯卡拉:
package stackOverflow
import stackOverflow._
class ScalaByteReceiver{
def receive(bytes: Array[Byte]) {
println(bytes.length);
// prints 4
}
}
因此 Java 和 Scala 可以很好地相互理解。
关于java - 如何将 Java 字节数组转换为 Scala 字节数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6237666/
我是一名优秀的程序员,十分优秀!