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 的元素,則無法保證會找到哪一個。
參數
fromIndex
- 要搜索的範圍(包括)的開始,默認為 0。
toIndex
- 要搜索的範圍的末尾(不包括),默認情況下此數組的大小。
異常
IndexOutOfBoundsException
- 如果從索引小於零或索引大於此數組的大小。
IllegalArgumentException
- 如果從索引大於索引.
返回元素的索引,如果它包含在指定範圍內的數組中;否則,反轉插入點 (-insertion point - 1)
。插入點被定義為應該插入元素的索引,因此數組(或數組的指定子範圍)仍然根據指定的 comparator 排序。
用法五
@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 的元素,則無法保證會找到哪一個。
參數
fromIndex
- 要搜索的範圍(包括)的開始,默認為 0。
toIndex
- 要搜索的範圍的末尾(不包括),默認情況下此數組的大小。
異常
IndexOutOfBoundsException
- 如果從索引小於零或索引大於此數組的大小。
IllegalArgumentException
- 如果從索引大於索引.
返回元素的索引,如果它包含在指定範圍內的數組中;否則,反轉插入點 (-insertion point - 1)
。插入點定義為應該插入元素的索引,因此數組(或數組的指定子範圍)仍然保持排序狀態。
相關用法
- Kotlin binarySearchBy用法及代碼示例
- Kotlin associateBy用法及代碼示例
- Kotlin all用法及代碼示例
- Kotlin map用法及代碼示例
- Kotlin filterNot用法及代碼示例
- Kotlin reduceRight用法及代碼示例
- Kotlin Random.Default用法及代碼示例
- Kotlin Byte.inc用法及代碼示例
- Kotlin getValue用法及代碼示例
- Kotlin Double.dec用法及代碼示例
- Kotlin windowedSequence用法及代碼示例
- Kotlin contentToString用法及代碼示例
- Kotlin groupByTo用法及代碼示例
- Kotlin commonPrefixWith用法及代碼示例
- Kotlin MatchResult.Destructured用法及代碼示例
- Kotlin Delegates.notNull用法及代碼示例
- Kotlin ifBlank用法及代碼示例
- Kotlin filterNotTo用法及代碼示例
- Kotlin getOrPut用法及代碼示例
- Kotlin Triple.<init>用法及代碼示例
- Kotlin Duration.toString用法及代碼示例
- Kotlin windowed用法及代碼示例
- Kotlin indexOf用法及代碼示例
- Kotlin reduceIndexed用法及代碼示例
- Kotlin filterKeys用法及代碼示例
注:本文由純淨天空篩選整理自kotlinlang.org大神的英文原創作品 kotlin.collections.binarySearch。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。