I am trying to wrap java.util.Array.binarySearch
in a generic fashion in Scala, but the following code doesn't work:
def binarySlice[T](minValue: T, array: Array[T]): Array[T] = {
val i = java.util.Arrays.binarySearch(array, minValue)
val idx = if (i > 0) i else -i - 1
array.slice(idx, array.length)
}
The error is: Cannot resolve overloaded method 'binarySearch'
. What should I do to make this code work?
See: Scala replacement for Arrays.binarySearch :
def binarySlice[T <: AnyRef](minValue: T, array: Array[T]): Array[T] = {
val i = java.util.Arrays.binarySearch(array.asInstanceOf[Array[AnyRef]], minValue)
val idx = if (i > 0) i else -i - 1
array.slice(idx, array.length)
}
You can not use it for primitives. It is work-around for int:
scala> binarySlice[java.lang.Integer](3, Array(0,3,7,8))
res6: Array[Integer] = Array(3, 7, 8)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.