copyOf
fun ShortArray.copyOf(): ShortArrayfun FloatArray.copyOf(): FloatArrayfun DoubleArray.copyOf(): DoubleArrayfun BooleanArray.copyOf(): BooleanArrayReturns new array which is a copy of the original array.
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val array = arrayOf("apples", "oranges", "limes")
val arrayCopy = array.copyOf()
println(arrayCopy.contentToString()) // [apples, oranges, limes]
//sampleEnd
}fun ShortArray.copyOf(newSize: Int): ShortArrayfun FloatArray.copyOf(newSize: Int): FloatArrayfun DoubleArray.copyOf(newSize: Int): DoubleArrayReturns new array which is a copy of the original array, resized to the given newSize. The copy is either truncated or padded at the end with zero values if necessary.
- If newSize is less than the size of the original array, the copy array is truncated to the newSize.
- If newSize is greater than the size of the original array, the extra elements in the copy array are filled with zero values.
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val array = intArrayOf(1, 2, 3)
val arrayCopyPadded = array.copyOf(5)
println(arrayCopyPadded.contentToString()) // [1, 2, 3, 0, 0]
val arrayCopyTruncated = array.copyOf(2)
println(arrayCopyTruncated.contentToString()) // [1, 2]
//sampleEnd
}fun BooleanArray.copyOf(newSize: Int): BooleanArrayReturns new array which is a copy of the original array, resized to the given newSize.
The copy is either truncated or padded at the end with false values if necessary.
- If newSize is less than the size of the original array, the copy array is truncated to the newSize.
- If newSize is greater than the size of the original array, the extra elements in the copy array are filled with
falsevalues.
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val array = intArrayOf(1, 2, 3)
val arrayCopyPadded = array.copyOf(5)
println(arrayCopyPadded.contentToString()) // [1, 2, 3, 0, 0]
val arrayCopyTruncated = array.copyOf(2)
println(arrayCopyTruncated.contentToString()) // [1, 2]
//sampleEnd
}Returns new array which is a copy of the original array, resized to the given newSize.
The copy is either truncated or padded at the end with null char (\u0000) values if necessary.
- If newSize is less than the size of the original array, the copy array is truncated to the newSize.
- If newSize is greater than the size of the original array, the extra elements in the copy array are filled with null char (
\u0000) values.
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val array = intArrayOf(1, 2, 3)
val arrayCopyPadded = array.copyOf(5)
println(arrayCopyPadded.contentToString()) // [1, 2, 3, 0, 0]
val arrayCopyTruncated = array.copyOf(2)
println(arrayCopyTruncated.contentToString()) // [1, 2]
//sampleEnd
}Returns new array which is a copy of the original array, resized to the given newSize.
The copy is either truncated or padded at the end with null values if necessary.
- If newSize is less than the size of the original array, the copy array is truncated to the newSize.
- If newSize is greater than the size of the original array, the extra elements in the copy array are filled with
nullvalues.
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val array = arrayOf("apples", "oranges", "limes")
val arrayCopyPadded = array.copyOf(5)
println(arrayCopyPadded.contentToString()) // [apples, oranges, limes, null, null]
val arrayCopyTruncated = array.copyOf(2)
println(arrayCopyTruncated.contentToString()) // [apples, oranges]
//sampleEnd
}@ExperimentalUnsignedTypes fun ULongArray.copyOf(): ULongArray@ExperimentalUnsignedTypes fun UByteArray.copyOf(): UByteArray@ExperimentalUnsignedTypes fun UShortArray.copyOf(): UShortArrayReturns new array which is a copy of the original array.
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val array = arrayOf("apples", "oranges", "limes")
val arrayCopy = array.copyOf()
println(arrayCopy.contentToString()) // [apples, oranges, limes]
//sampleEnd
}@ExperimentalUnsignedTypes fun ULongArray.copyOf(
newSize: Int
): ULongArray@ExperimentalUnsignedTypes fun UByteArray.copyOf(
newSize: Int
): UByteArray@ExperimentalUnsignedTypes fun UShortArray.copyOf(
newSize: Int
): UShortArrayReturns new array which is a copy of the original array, resized to the given newSize. The copy is either truncated or padded at the end with zero values if necessary.