copyOf

Common
inline fun UIntArray.copyOf(): UIntArray
inline fun ULongArray.copyOf(): ULongArray
inline fun UByteArray.copyOf(): UByteArray
inline fun UShortArray.copyOf(): UShortArray

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

Since Kotlin

1.3

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val array = arrayOf("apples", "oranges", "limes")
val arrayCopy = array.copyOf()
assertPrints(arrayCopy.contentToString(), "[apples, oranges, limes]") 
   //sampleEnd
}
Common
inline fun UIntArray.copyOf(newSize: Int): UIntArray
inline fun ULongArray.copyOf(newSize: Int): ULongArray
inline fun UByteArray.copyOf(newSize: Int): UByteArray
inline 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.

Since Kotlin

1.3
Common
fun <T> Array<T>.copyOf(): Array<T>
fun ByteArray.copyOf(): ByteArray
fun ShortArray.copyOf(): ShortArray
fun IntArray.copyOf(): IntArray
fun LongArray.copyOf(): LongArray
fun FloatArray.copyOf(): FloatArray
fun DoubleArray.copyOf(): DoubleArray
fun BooleanArray.copyOf(): BooleanArray
fun CharArray.copyOf(): CharArray

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

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val array = arrayOf("apples", "oranges", "limes")
val arrayCopy = array.copyOf()
assertPrints(arrayCopy.contentToString(), "[apples, oranges, limes]") 
   //sampleEnd
}
inline fun <T> Array<T>.copyOf(): Array<T>
inline fun ByteArray.copyOf(): ByteArray
inline fun ShortArray.copyOf(): ShortArray
inline fun IntArray.copyOf(): IntArray
inline fun LongArray.copyOf(): LongArray
inline fun FloatArray.copyOf(): FloatArray
inline fun DoubleArray.copyOf(): DoubleArray
inline fun BooleanArray.copyOf(): BooleanArray
inline fun CharArray.copyOf(): CharArray

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

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val array = arrayOf("apples", "oranges", "limes")
val arrayCopy = array.copyOf()
assertPrints(arrayCopy.contentToString(), "[apples, oranges, limes]") 
   //sampleEnd
}
JS
inline fun <T> Array<T>.copyOf(): Array<T>
inline fun ByteArray.copyOf(): ByteArray
inline fun ShortArray.copyOf(): ShortArray
inline fun IntArray.copyOf(): IntArray
fun LongArray.copyOf(): LongArray
inline fun FloatArray.copyOf(): FloatArray
inline fun DoubleArray.copyOf(): DoubleArray
fun BooleanArray.copyOf(): BooleanArray
fun CharArray.copyOf(): CharArray

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

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val array = arrayOf("apples", "oranges", "limes")
val arrayCopy = array.copyOf()
assertPrints(arrayCopy.contentToString(), "[apples, oranges, limes]") 
   //sampleEnd
}
Native
fun <T> Array<T>.copyOf(): Array<T>
fun ByteArray.copyOf(): ByteArray
fun ShortArray.copyOf(): ShortArray
fun IntArray.copyOf(): IntArray
fun LongArray.copyOf(): LongArray
fun FloatArray.copyOf(): FloatArray
fun DoubleArray.copyOf(): DoubleArray
fun BooleanArray.copyOf(): BooleanArray
fun CharArray.copyOf(): CharArray

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

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val array = arrayOf("apples", "oranges", "limes")
val arrayCopy = array.copyOf()
assertPrints(arrayCopy.contentToString(), "[apples, oranges, limes]") 
   //sampleEnd
}
Common
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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val array = intArrayOf(1, 2, 3)
val arrayCopyPadded = array.copyOf(5)
assertPrints(arrayCopyPadded.contentToString(), "[1, 2, 3, 0, 0]")
val arrayCopyTruncated = array.copyOf(2)
assertPrints(arrayCopyTruncated.contentToString(), "[1, 2]") 
   //sampleEnd
}
inline fun ByteArray.copyOf(newSize: Int): ByteArray
inline fun ShortArray.copyOf(newSize: Int): ShortArray
inline fun IntArray.copyOf(newSize: Int): IntArray
inline fun LongArray.copyOf(newSize: Int): LongArray
inline fun FloatArray.copyOf(newSize: Int): FloatArray
inline 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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val array = intArrayOf(1, 2, 3)
val arrayCopyPadded = array.copyOf(5)
assertPrints(arrayCopyPadded.contentToString(), "[1, 2, 3, 0, 0]")
val arrayCopyTruncated = array.copyOf(2)
assertPrints(arrayCopyTruncated.contentToString(), "[1, 2]") 
   //sampleEnd
}
JS
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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val array = intArrayOf(1, 2, 3)
val arrayCopyPadded = array.copyOf(5)
assertPrints(arrayCopyPadded.contentToString(), "[1, 2, 3, 0, 0]")
val arrayCopyTruncated = array.copyOf(2)
assertPrints(arrayCopyTruncated.contentToString(), "[1, 2]") 
   //sampleEnd
}
Native
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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val array = intArrayOf(1, 2, 3)
val arrayCopyPadded = array.copyOf(5)
assertPrints(arrayCopyPadded.contentToString(), "[1, 2, 3, 0, 0]")
val arrayCopyTruncated = array.copyOf(2)
assertPrints(arrayCopyTruncated.contentToString(), "[1, 2]") 
   //sampleEnd
}
Common
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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val array = intArrayOf(1, 2, 3)
val arrayCopyPadded = array.copyOf(5)
assertPrints(arrayCopyPadded.contentToString(), "[1, 2, 3, 0, 0]")
val arrayCopyTruncated = array.copyOf(2)
assertPrints(arrayCopyTruncated.contentToString(), "[1, 2]") 
   //sampleEnd
}
inline 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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val array = intArrayOf(1, 2, 3)
val arrayCopyPadded = array.copyOf(5)
assertPrints(arrayCopyPadded.contentToString(), "[1, 2, 3, 0, 0]")
val arrayCopyTruncated = array.copyOf(2)
assertPrints(arrayCopyTruncated.contentToString(), "[1, 2]") 
   //sampleEnd
}
JS
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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val array = intArrayOf(1, 2, 3)
val arrayCopyPadded = array.copyOf(5)
assertPrints(arrayCopyPadded.contentToString(), "[1, 2, 3, 0, 0]")
val arrayCopyTruncated = array.copyOf(2)
assertPrints(arrayCopyTruncated.contentToString(), "[1, 2]") 
   //sampleEnd
}
Native
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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val array = intArrayOf(1, 2, 3)
val arrayCopyPadded = array.copyOf(5)
assertPrints(arrayCopyPadded.contentToString(), "[1, 2, 3, 0, 0]")
val arrayCopyTruncated = array.copyOf(2)
assertPrints(arrayCopyTruncated.contentToString(), "[1, 2]") 
   //sampleEnd
}
Common
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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val array = intArrayOf(1, 2, 3)
val arrayCopyPadded = array.copyOf(5)
assertPrints(arrayCopyPadded.contentToString(), "[1, 2, 3, 0, 0]")
val arrayCopyTruncated = array.copyOf(2)
assertPrints(arrayCopyTruncated.contentToString(), "[1, 2]") 
   //sampleEnd
}
inline 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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val array = intArrayOf(1, 2, 3)
val arrayCopyPadded = array.copyOf(5)
assertPrints(arrayCopyPadded.contentToString(), "[1, 2, 3, 0, 0]")
val arrayCopyTruncated = array.copyOf(2)
assertPrints(arrayCopyTruncated.contentToString(), "[1, 2]") 
   //sampleEnd
}
JS
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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val array = intArrayOf(1, 2, 3)
val arrayCopyPadded = array.copyOf(5)
assertPrints(arrayCopyPadded.contentToString(), "[1, 2, 3, 0, 0]")
val arrayCopyTruncated = array.copyOf(2)
assertPrints(arrayCopyTruncated.contentToString(), "[1, 2]") 
   //sampleEnd
}
Native
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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val array = intArrayOf(1, 2, 3)
val arrayCopyPadded = array.copyOf(5)
assertPrints(arrayCopyPadded.contentToString(), "[1, 2, 3, 0, 0]")
val arrayCopyTruncated = array.copyOf(2)
assertPrints(arrayCopyTruncated.contentToString(), "[1, 2]") 
   //sampleEnd
}
Common
fun <T> Array<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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val array = arrayOf("apples", "oranges", "limes")
val arrayCopyPadded = array.copyOf(5)
assertPrints(arrayCopyPadded.contentToString(), "[apples, oranges, limes, null, null]")
val arrayCopyTruncated = array.copyOf(2)
assertPrints(arrayCopyTruncated.contentToString(), "[apples, oranges]") 
   //sampleEnd
}
inline fun <T> Array<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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val array = arrayOf("apples", "oranges", "limes")
val arrayCopyPadded = array.copyOf(5)
assertPrints(arrayCopyPadded.contentToString(), "[apples, oranges, limes, null, null]")
val arrayCopyTruncated = array.copyOf(2)
assertPrints(arrayCopyTruncated.contentToString(), "[apples, oranges]") 
   //sampleEnd
}
JS
fun <T> Array<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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val array = arrayOf("apples", "oranges", "limes")
val arrayCopyPadded = array.copyOf(5)
assertPrints(arrayCopyPadded.contentToString(), "[apples, oranges, limes, null, null]")
val arrayCopyTruncated = array.copyOf(2)
assertPrints(arrayCopyTruncated.contentToString(), "[apples, oranges]") 
   //sampleEnd
}
Native
fun <T> Array<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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val array = arrayOf("apples", "oranges", "limes")
val arrayCopyPadded = array.copyOf(5)
assertPrints(arrayCopyPadded.contentToString(), "[apples, oranges, limes, null, null]")
val arrayCopyTruncated = array.copyOf(2)
assertPrints(arrayCopyTruncated.contentToString(), "[apples, oranges]") 
   //sampleEnd
}