synapse
synapse

Reputation: 5728

Converting Scala array of Int to Java array of Integer

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

Answers (1)

scand1sk
scand1sk

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

Related Questions