binarySearch所在位置是kotlin.collections.binarySearch,其相关用法介绍如下。

用法一

fun <T : Comparable<T>> List<T?>.binarySearch(
    element: T?, 
    fromIndex: Int = 0, 
    toIndex: Int = size
): Int

使用二分搜索算法在此列表或其范围中搜索提供的element。该列表应根据其元素的 Comparable 自然排序进行升序排序,否则结果未定义。

如果列表包含多个等于指定 element 的元素,则无法保证会找到哪一个。

null 值被认为小于任何非空值。

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = mutableListOf('a', 'b', 'c', 'd', 'e')
println(list.binarySearch('d')) // 3

list.remove('d')

val invertedInsertionPoint = list.binarySearch('d')
val actualInsertionPoint = -(invertedInsertionPoint + 1)
println(actualInsertionPoint) // 3

list.add(actualInsertionPoint, 'd')
println(list) // [a, b, c, d, e]
//sampleEnd
}

输出:

3
3
[a, b, c, d, e]

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf('a', 'b', 'c', 'd', 'e')
println(list.binarySearch('d')) // 3

// element is out of range from the left
println("list.binarySearch('b', fromIndex = 2) < 0 is ${list.binarySearch('b', fromIndex = 2) < 0}") // true

// element is out of range from the right
println("list.binarySearch('d', toIndex = 2) < 0 is ${list.binarySearch('d', toIndex = 2) < 0}") // true
//sampleEnd
}

输出:

3
list.binarySearch('b', fromIndex = 2) < 0 is true
list.binarySearch('d', toIndex = 2) < 0 is true

返回元素的索引,如果它包含在指定范围内的列表中;否则,反转插入点 (-insertion point - 1) 。插入点被定义为应该插入元素的索引,因此列表(或列表的指定子范围)仍然保持排序状态。

用法二

fun <T> List<T>.binarySearch(
    element: T, 
    comparator: Comparator<in T>, 
    fromIndex: Int = 0, 
    toIndex: Int = size
): Int

使用二分搜索算法在此列表或其范围中搜索提供的element。该列表应按照指定的 comparator 升序排序,否则结果未定义。

如果列表包含多个等于指定 element 的元素,则无法保证会找到哪一个。

null 值被认为小于任何非空值。

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val colors = listOf("Blue", "green", "ORANGE", "Red", "yellow")
println(colors.binarySearch("RED", String.CASE_INSENSITIVE_ORDER)) // 3
//sampleEnd
}

输出:

3

返回元素的索引,如果它包含在指定范围内的列表中;否则,反转插入点 (-insertion point - 1) 。插入点定义为应该插入元素的索引,因此列表(或列表的指定子范围)仍然根据指定的 comparator 进行排序。

用法三

fun <T> List<T>.binarySearch(
    fromIndex: Int = 0, 
    toIndex: Int = size, 
    comparison: (T) -> Int
): Int

使用二分搜索算法在此列表或其范围中搜索给定 comparison 函数返回零的元素。

预计列表将被排序,以便 comparison 函数的返回值的符号在列表元素上上升,即负值在零之前,零在正值之前。否则,结果是未定义的。

如果列表包含多个comparison 返回零的元素,则无法保证会找到哪一个。

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
data class Box(val value: String)

val values = listOf("A", "ant", "binding", "Box", "cell")
val boxes = values.map { Box(it) }

val valueToFind = "box"
// `boxes` list is sorted according to the following comparison function
val index = boxes.binarySearch { String.CASE_INSENSITIVE_ORDER.compare(it.value, valueToFind) }

if (index >= 0) {
    println("Value at $index is ${boxes[index]}") // Value at 3 is Box(value=Box)
} else {
    println("Box with value=$valueToFind was not found")
}
//sampleEnd
}

输出:

Value at 3 is Box(value=Box)

参数

comparison- 在被搜索的列表元素上调用时返回零的函数。在目标元素之前的元素上,函数必须返回负值;在目标元素之后的元素上,函数必须返回正值。

返回找到的元素的索引,如果它包含在指定范围内的列表中;否则,反转插入点 (-insertion point - 1) 。插入点被定义为应该插入元素的索引,因此列表(或列表的指定子范围)仍然保持排序状态。

用法四

fun <T> Array<out T>.binarySearch(
    element: T, 
    comparator: Comparator<in T>, 
    fromIndex: Int = 0, 
    toIndex: Int = size
): Int

使用二分搜索算法在数组或数组范围内搜索提供的element。该数组应根据指定的 comparator 进行排序,否则结果未定义。

如果数组包含多个等于指定 element 的元素,则无法保证会找到哪一个。

参数

element- 要搜索的元素。

comparator- 排序此数组所依据的比较器。

fromIndex- 要搜索的范围(包括)的开始,默认为 0。

toIndex- 要搜索的范围的末尾(不包括),默认情况下此数组的大小。

异常

IndexOutOfBoundsException- 如果从索引小于零或索引大于此数组的大小。

IllegalArgumentException- 如果从索引大于索引.

返回元素的索引,如果它包含在指定范围内的数组中;否则,反转插入点 (-insertion point - 1) 。插入点被定义为应该插入元素的索引,因此数组(或数组的指定子范围)仍然根据指定的 comparator 排序。

用法五

fun <T> Array<out T>.binarySearch(
    element: T, 
    fromIndex: Int = 0, 
    toIndex: Int = size
): Int
fun ByteArray.binarySearch(
    element: Byte, 
    fromIndex: Int = 0, 
    toIndex: Int = size
): Int
fun ShortArray.binarySearch(
    element: Short, 
    fromIndex: Int = 0, 
    toIndex: Int = size
): Int
fun IntArray.binarySearch(
    element: Int, 
    fromIndex: Int = 0, 
    toIndex: Int = size
): Int
fun LongArray.binarySearch(
    element: Long, 
    fromIndex: Int = 0, 
    toIndex: Int = size
): Int
fun FloatArray.binarySearch(
    element: Float, 
    fromIndex: Int = 0, 
    toIndex: Int = size
): Int
fun DoubleArray.binarySearch(
    element: Double, 
    fromIndex: Int = 0, 
    toIndex: Int = size
): Int
fun CharArray.binarySearch(
    element: Char, 
    fromIndex: Int = 0, 
    toIndex: Int = size
): Int
@ExperimentalUnsignedTypes fun UIntArray.binarySearch(
    element: UInt, 
    fromIndex: Int = 0, 
    toIndex: Int = size
): Int
@ExperimentalUnsignedTypes fun ULongArray.binarySearch(
    element: ULong, 
    fromIndex: Int = 0, 
    toIndex: Int = size
): Int
@ExperimentalUnsignedTypes fun UByteArray.binarySearch(
    element: UByte, 
    fromIndex: Int = 0, 
    toIndex: Int = size
): Int
@ExperimentalUnsignedTypes fun UShortArray.binarySearch(
    element: UShort, 
    fromIndex: Int = 0, 
    toIndex: Int = size
): Int

使用二分搜索算法在数组或数组范围内搜索提供的element。该数组应被排序,否则结果未定义。

如果数组包含多个等于指定 element 的元素,则无法保证会找到哪一个。

参数

element- 要搜索的。

fromIndex- 要搜索的范围(包括)的开始,默认为 0。

toIndex- 要搜索的范围的末尾(不包括),默认情况下此数组的大小。

异常

IndexOutOfBoundsException- 如果从索引小于零或索引大于此数组的大小。

IllegalArgumentException- 如果从索引大于索引.

返回元素的索引,如果它包含在指定范围内的数组中;否则,反转插入点 (-insertion point - 1) 。插入点定义为应该插入元素的索引,因此数组(或数组的指定子范围)仍然保持排序状态。