Kotlin Array copyInto Function

The copyInto function in Kotlin is used to copy elements from one array into another array. This function is part of the Kotlin standard library and provides a straightforward way to duplicate elements from a source array into a destination array, either partially or completely.

Table of Contents

  1. Introduction
  2. copyInto Function Syntax
  3. Understanding copyInto
  4. Examples
    • Basic Usage
    • Using copyInto with Specified Ranges
    • Handling Overlapping Arrays
  5. Real-World Use Case
  6. Conclusion

Introduction

The copyInto function copies elements from the source array into the destination array. It allows specifying the range of elements to copy and the position in the destination array where the elements should be placed.

copyInto Function Syntax

The syntax for the copyInto function is as follows:

fun <T> Array<out T>.copyInto(
    destination: Array<T>,
    destinationOffset: Int = 0,
    startIndex: Int = 0,
    endIndex: Int = this.size
): Array<T>

Parameters:

  • destination: The array into which elements will be copied.
  • destinationOffset: The starting index in the destination array (default is 0).
  • startIndex: The starting index in the source array (default is 0).
  • endIndex: The ending index in the source array (exclusive, default is the size of the source array).

Returns:

  • The destination array with the copied elements.

Understanding copyInto

The copyInto function allows copying elements from a specified range in the source array to a specified position in the destination array. It is useful for array manipulation tasks where elements need to be transferred between arrays.

Examples

Basic Usage

To demonstrate the basic usage of copyInto, we will create a source array and copy its elements into a destination array.

Example

fun main() {
    val source = arrayOf(1, 2, 3, 4, 5)
    val destination = arrayOf(0, 0, 0, 0, 0)
    source.copyInto(destination)
    println("Source array: ${source.joinToString()}")
    println("Destination array: ${destination.joinToString()}")
}

Output:

Source array: 1, 2, 3, 4, 5
Destination array: 1, 2, 3, 4, 5

Using copyInto with Specified Ranges

This example shows how to use copyInto to copy a range of elements from the source array into the destination array at a specified position.

Example

fun main() {
    val source = arrayOf(1, 2, 3, 4, 5)
    val destination = arrayOf(0, 0, 0, 0, 0, 0, 0)
    source.copyInto(destination, destinationOffset = 2, startIndex = 1, endIndex = 4)
    println("Source array: ${source.joinToString()}")
    println("Destination array: ${destination.joinToString()}")
}

Output:

Source array: 1, 2, 3, 4, 5
Destination array: 0, 0, 2, 3, 4, 0, 0

Handling Overlapping Arrays

This example demonstrates how to use copyInto to handle overlapping arrays, where the source and destination arrays are the same.

Example

fun main() {
    val array = arrayOf(1, 2, 3, 4, 5)
    array.copyInto(array, destinationOffset = 1, startIndex = 0, endIndex = 4)
    println("Array after overlapping copy: ${array.joinToString()}")
}

Output:

Array after overlapping copy: 1, 1, 2, 3, 4

Real-World Use Case

Merging Arrays with copyInto

In real-world applications, the copyInto function can be used to merge elements from one array into another, such as combining datasets or updating parts of an array.

Example

fun main() {
    val dataset1 = arrayOf(1, 2, 3, 4, 5)
    val dataset2 = arrayOf(6, 7, 8, 9, 10)
    val merged = arrayOfNulls<Int>(10)

    dataset1.copyInto(merged, destinationOffset = 0)
    dataset2.copyInto(merged, destinationOffset = 5)

    println("Merged dataset: ${merged.joinToString()}")
}

Output:

Merged dataset: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Conclusion

The copyInto function in Kotlin is a versatile method for copying elements from one array to another. It allows for specifying ranges and handling overlapping arrays, making it useful for various array manipulation tasks. By understanding and using this function, you can effectively manage array copying and merging operations in your Kotlin applications.

Comments