Kotlin Array copyOf Function

The copyOf function in Kotlin is used to create a new array that is a copy of an existing array. This function is part of the Kotlin standard library and provides a straightforward way to duplicate arrays with optional resizing.

Table of Contents

  1. Introduction
  2. copyOf Function Syntax
  3. Understanding copyOf
  4. Examples
    • Basic Usage
    • Copying with Resizing
    • Using copyOf with Custom Types
  5. Real-World Use Case
  6. Conclusion

Introduction

The copyOf function creates a new array that is a copy of the original array. It can also create a new array with a specified size, either truncating or padding the original array as needed. This is useful for creating duplicates or resized versions of arrays.

copyOf Function Syntax

The syntax for the copyOf function is as follows:

fun <T> Array<out T>.copyOf(): Array<T>
fun <T> Array<out T>.copyOf(newSize: Int): Array<T>

Parameters:

  • newSize (optional): The new size for the copied array. If specified, the new array will be resized to this size.

Returns:

  • A new array that is a copy of the original array.

Understanding copyOf

The copyOf function creates a new array with the same elements as the original array. If a new size is specified, the resulting array will be resized accordingly. If the new size is larger, the new array will be padded with default values (null for object arrays). If the new size is smaller, the new array will be truncated to the specified size.

Examples

Basic Usage

To demonstrate the basic usage of copyOf, we will create an array of integers and make a copy of it.

Example

fun main() {
    val numbers = arrayOf(1, 2, 3, 4, 5)
    val numbersCopy = numbers.copyOf()
    println("Original array: ${numbers.joinToString()}")
    println("Copied array: ${numbersCopy.joinToString()}")
}

Output:

Original array: 1, 2, 3, 4, 5
Copied array: 1, 2, 3, 4, 5

Copying with Resizing

This example shows how to use copyOf to create a new array with a different size than the original array.

Example

fun main() {
    val numbers = arrayOf(1, 2, 3, 4, 5)
    val numbersCopyLarger = numbers.copyOf(7)
    val numbersCopySmaller = numbers.copyOf(3)
    println("Original array: ${numbers.joinToString()}")
    println("Copied array (larger): ${numbersCopyLarger.joinToString()}")
    println("Copied array (smaller): ${numbersCopySmaller.joinToString()}")
}

Output:

Original array: 1, 2, 3, 4, 5
Copied array (larger): 1, 2, 3, 4, 5, null, null
Copied array (smaller): 1, 2, 3

Using copyOf with Custom Types

This example demonstrates how to use copyOf with an array of custom objects.

Example

data class Person(val name: String, val age: Int)

fun main() {
    val people = arrayOf(
        Person("Ravi", 25),
        Person("Anjali", 30),
        Person("Priya", 22)
    )

    val peopleCopy = people.copyOf()
    println("Original array: ${people.joinToString()}")
    println("Copied array: ${peopleCopy.joinToString()}")
}

Output:

Original array: Person(name='Ravi', age=25), Person(name='Anjali', age=30), Person(name='Priya', age=22)
Copied array: Person(name='Ravi', age=25), Person(name='Anjali', age=30), Person(name='Priya', age=22)

Real-World Use Case

Creating a Resized Copy of an Array

In real-world applications, the copyOf function can be used to create a resized copy of an array, such as creating a larger array to accommodate additional elements.

Example

fun main() {
    val originalArray = arrayOf(1, 2, 3)
    val resizedArray = originalArray.copyOf(5)

    // Add new elements to the resized array
    resizedArray[3] = 4
    resizedArray[4] = 5

    println("Original array: ${originalArray.joinToString()}")
    println("Resized array: ${resizedArray.joinToString()}")
}

Output:

Original array: 1, 2, 3
Resized array: 1, 2, 3, 4, 5

Conclusion

The copyOf function in Kotlin is a convenient method for creating copies of arrays, with optional resizing. It provides a simple way to duplicate arrays and handle cases where a resized array is needed. 

By understanding and using this function, you can effectively manage array duplication and resizing operations in your Kotlin applications.

Comments