Is there a replacement in Scala for Java's int Arrays.binarySearch(Object[] array, object)
?
The problem is that Scala's Arrays are not covariant, so I would have to cast my stringArray: Array[String]
like this first:
stringArray.asInstanceOf[Array[Object]]
Is there a better solution?
Returns. The index of the specified value in the specified array , if value is found; otherwise, a negative number. If value is not found and value is less than one or more elements in array , the negative number returned is the bitwise complement of the index of the first element that is larger than value .
Scala provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Scala 2.11 added scala.collection.Searching
to the standard library. It uses binary search for indexed sequences and linear search otherwise.
import scala.collection.Searching._ Array(1, 2, 3, 4, 5).search(3)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With