Optimize K/N ArrayList.toArray #KT-42720
This commit is contained in:
@@ -164,6 +164,26 @@ actual class ArrayList<E> private constructor(
|
||||
return array.subarrayContentToString(offset, length)
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T> toArray(destination: Array<T>): Array<T> {
|
||||
if (destination.size < length) {
|
||||
return array.copyOfRange(fromIndex = offset, toIndex = offset + length) as Array<T>
|
||||
}
|
||||
|
||||
(array as Array<T>).copyInto(destination, 0, startIndex = offset, endIndex = offset + length)
|
||||
|
||||
if (destination.size > length) {
|
||||
destination[length] = null as T // null-terminate
|
||||
}
|
||||
|
||||
return destination
|
||||
}
|
||||
|
||||
override fun toArray(): Array<Any?> {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return array.copyOfRange(fromIndex = offset, toIndex = offset + length) as Array<Any?>
|
||||
}
|
||||
|
||||
// ---------------------------- private ----------------------------
|
||||
|
||||
private fun checkElementIndex(index: Int) {
|
||||
|
||||
Reference in New Issue
Block a user