Reputation: 5728
How to convert Scala Array[Int]
to Java Integer[]
? It seems that the default is to convert int to int[]
and that's not a proper argument for method defined as
public static <T extends Comparable<? super T>> T[] merge(T[] xs)
Compiling fails with the following error
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)
^
Upvotes: 3
Views: 5392
Reputation: 1124
Maybe with:
val javaArray: Array[java.lang.Integer] = scalaArray map java.lang.Integer.valueOf
AFAIK, it is not a good practice in java to manipulate array of objects, you should use Lists instead. BTW, your merge
method will have trouble instantiating the array of T
.
Upvotes: 5