W3cubDocs

/Kotlin

copyOf

Platform and version requirements: JVM (1.0), Native (1.3)
fun <T> Array<T>.copyOf(): Array<T>
Platform and version requirements: JS (1.1)
fun <T> Array<out T>.copyOf(): Array<T>
Platform and version requirements: JVM (1.0), JS (1.1), Native (1.3)
fun ByteArray.copyOf(): ByteArray
Platform and version requirements: JVM (1.0), JS (1.1), Native (1.3)
fun ShortArray.copyOf(): ShortArray
Platform and version requirements: JVM (1.0), JS (1.1), Native (1.3)
fun IntArray.copyOf(): IntArray
Platform and version requirements: JVM (1.0), JS (1.1), Native (1.3)
fun LongArray.copyOf(): LongArray
Platform and version requirements: JVM (1.0), JS (1.1), Native (1.3)
fun FloatArray.copyOf(): FloatArray
Platform and version requirements: JVM (1.0), JS (1.1), Native (1.3)
fun DoubleArray.copyOf(): DoubleArray
Platform and version requirements: JVM (1.0), JS (1.1), Native (1.3)
fun BooleanArray.copyOf(): BooleanArray
Platform and version requirements: JVM (1.0), JS (1.1), Native (1.3)
fun CharArray.copyOf(): CharArray

Returns 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
}
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun ByteArray.copyOf(newSize: Int): ByteArray
fun ShortArray.copyOf(newSize: Int): ShortArray
fun IntArray.copyOf(newSize: Int): IntArray
fun LongArray.copyOf(newSize: Int): LongArray
fun FloatArray.copyOf(newSize: Int): FloatArray
fun DoubleArray.copyOf(newSize: Int): DoubleArray

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 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
}
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun BooleanArray.copyOf(newSize: Int): BooleanArray

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 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 false 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
}
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun CharArray.copyOf(newSize: Int): CharArray

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
}
Platform and version requirements: JVM (1.0), Native (1.3)
fun <T> Array<T>.copyOf(newSize: Int): Array<T?>
Platform and version requirements: JS (1.1)
fun <T> Array<out T>.copyOf(newSize: Int): Array<T?>

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 null values.
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
}
Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)
@ExperimentalUnsignedTypes fun UIntArray.copyOf(): UIntArray
@ExperimentalUnsignedTypes fun ULongArray.copyOf(): ULongArray
@ExperimentalUnsignedTypes fun UByteArray.copyOf(): UByteArray
@ExperimentalUnsignedTypes fun UShortArray.copyOf(): UShortArray

Returns 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
}
Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)
@ExperimentalUnsignedTypes fun UIntArray.copyOf(
    newSize: Int
): UIntArray
@ExperimentalUnsignedTypes fun ULongArray.copyOf(
    newSize: Int
): ULongArray
@ExperimentalUnsignedTypes fun UByteArray.copyOf(
    newSize: Int
): UByteArray
@ExperimentalUnsignedTypes fun UShortArray.copyOf(
    newSize: Int
): UShortArray

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 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.

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/copy-of.html