如何将Scala Array[Int]
转换为Java Integer[]
?似乎默认是将int转换为int[]
,并且这不是定义为
public static <T extends Comparable<? super T>> T[] merge(T[] xs)
编译失败,出现以下错误
type mismatch;
found : Array[Int]
required: Array[? with Object]
Note: Int >: ? with Object, but class Array is invariant in type T.
You may wish to investigate a wildcard type such as `_ >: ? with Object`. (SLS 3.2.10)
val res = SimpleSort.merge(xs)
^
答案 0 :(得分:4)
可能与:
val javaArray: Array[java.lang.Integer] = scalaArray map java.lang.Integer.valueOf
AFAIK,在java中操作对象数组不是一个好习惯,你应该使用Lists代替。顺便说一句,您的merge
方法在实例化T
数组时会遇到问题。