Copy a two-dimensional array in Kotlin
This article explores different ways to copy two-dimensional arrays in Kotlin. The column size may or may not be fixed for the two-dimensional array.
1. Using clone() function
The idea is to iterate over each row of the original array and then call the clone() function to clone each row.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
fun clone(array: Array<IntArray>?): Array<IntArray?>? { if (array == null) { return null } val clone = arrayOfNulls<IntArray>(array.size) for (i in array.indices) { clone[i] = array[i].clone() } return clone } fun main() { val array = arrayOf(intArrayOf(1, 2, 3, 4, 5), intArrayOf(6, 7, 8, 9), intArrayOf(10, 11, 12)) val clone = clone(array) clone?.forEach { println(it?.contentToString()) } } |
Output:
[1, 2, 3, 4, 5]
[6, 7, 8, 9]
[10, 11, 12]
Alternatively, we can use the map() function to shorten the code and improve its readability:
|
1 2 3 4 5 6 7 |
fun main() { val array = arrayOf(intArrayOf(1, 2, 3, 4, 5), intArrayOf(6, 7, 8, 9), intArrayOf(10, 11, 12)) val clone = array.map { it.clone() }.toTypedArray() clone.forEach { println(it.contentToString()) } } |
Output:
[1, 2, 3, 4, 5]
[6, 7, 8, 9]
[10, 11, 12]
2. Using System.arraycopy() function
We can also use the System.arraycopy() function to clone each row of the two-dimensional array. The following code example demonstrates the invocation of the System.arraycopy() function to create a copy of a two-dimensional array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
fun clone(array: Array<IntArray>?): Array<IntArray?>? { if (array == null) { return null } val clone = arrayOfNulls<IntArray>(array.size) for (i in array.indices) { clone[i] = IntArray(array[i].size) System.arraycopy(array[i], 0, clone[i], 0, array[i].size) } return clone } fun main() { val array = arrayOf(intArrayOf(1, 2, 3, 4, 5), intArrayOf(6, 7, 8, 9), intArrayOf(10, 11, 12)) val clone = clone(array) clone?.forEach { println(it?.contentToString()) } } |
Output:
[1, 2, 3, 4, 5]
[6, 7, 8, 9]
[10, 11, 12]
3. Using copyOf() function
Since all elements in each row are copied to the new array, the System.arraycopy() call can be avoided. We can improve the readability of the above code by using the copyOf() function. Here’s an equivalent version using the copyOf() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
fun clone(array: Array<IntArray>?): Array<IntArray?>? { if (array == null) { return null } val clone = arrayOfNulls<IntArray>(array.size) for (i in array.indices) { clone[i] = array[i].copyOf(array[i].size) } return clone } fun main() { val array = arrayOf(intArrayOf(1, 2, 3, 4, 5), intArrayOf(6, 7, 8, 9), intArrayOf(10, 11, 12)) val clone = clone(array) clone?.forEach { println(it?.contentToString()) } } |
Output:
[1, 2, 3, 4, 5]
[6, 7, 8, 9]
[10, 11, 12]
4. Nested Loop
Finally, we can use the nested for-loop for copying a two-dimensional array in Kotlin. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
fun clone(array: Array<IntArray>?): Array<IntArray>? { if (array == null) { return null } if (array.isEmpty()) { return Array(0) { IntArray(0) } } val clone = Array(array.size) { IntArray(0) } for (i in array.indices) { clone[i] = IntArray(array[i].size) for (j in array[i].indices) { clone[i][j] = array[i][j] } } return clone } fun main() { val array = arrayOf(intArrayOf(1, 2, 3, 4, 5), intArrayOf(6, 7, 8, 9), intArrayOf(10, 11, 12)) val clone = clone(array) clone?.forEach { println(it.contentToString()) } } |
Output:
[1, 2, 3, 4, 5]
[6, 7, 8, 9]
[10, 11, 12]
That’s all about copying a two-dimensional array in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)