copyOf

inline actual fun <T> Array<T>.copyOf(): Array<T> (source)
inline actual fun ByteArray.copyOf(): ByteArray (source)
inline actual fun ShortArray.copyOf(): ShortArray (source)
inline actual fun IntArray.copyOf(): IntArray (source)
inline actual fun LongArray.copyOf(): LongArray (source)
inline actual fun FloatArray.copyOf(): FloatArray (source)
inline actual fun DoubleArray.copyOf(): DoubleArray (source)
inline actual fun BooleanArray.copyOf(): BooleanArray (source)
inline actual fun CharArray.copyOf(): CharArray (source)

Returns new array which is a copy of the original array.



fun main(args: Array<String>) {
//sampleStart
val array = arrayOf("apples", "oranges", "limes")
val arrayCopy = array.copyOf()
println(arrayCopy.contentToString()) // [apples, oranges, limes]
//sampleEnd
}
inline actual fun ByteArray.copyOf(newSize: Int): ByteArray (source)
inline actual fun ShortArray.copyOf(newSize: Int): ShortArray (source)
inline actual fun IntArray.copyOf(newSize: Int): IntArray (source)
inline actual fun LongArray.copyOf(newSize: Int): LongArray (source)
inline actual fun FloatArray.copyOf(newSize: Int): FloatArray (source)
inline actual fun DoubleArray.copyOf(
    newSize: Int
): DoubleArray
(source)

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.


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
}
inline actual fun BooleanArray.copyOf(
    newSize: Int
): BooleanArray
(source)

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.


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
}
inline actual fun CharArray.copyOf(newSize: Int): CharArray (source)

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.


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
}
inline actual fun <T> Array<T>.copyOf(
    newSize: Int
): Array<T?>
(source)

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.


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
}