Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

                             ^
like image 811
synapse Avatar asked Nov 27 '25 06:11

synapse


1 Answers

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.

like image 97
scand1sk Avatar answered Nov 28 '25 20:11

scand1sk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!