To join arrays in Kotlin, you can use the plus
operator or the plus
method to concatenate two arrays into a single array.
In this example,
arr1
and arr2
with integer values.plus
operator to join arr1
and arr2
into a single array named result
.plus
operator concatenates the two arrays, resulting in a new array that contains all the elements of both arrays.fun main(args: Array<String>) {
val arr1 = arrayOf(1, 2, 3)
val arr2 = arrayOf(4, 5, 6)
val result = arr1 + arr2
println("Joined array: ${result.joinToString()}")
}
Joined array: 1, 2, 3, 4, 5, 6
In this example,
arr1
and arr2
with string values.plus
method to join arr1
and arr2
into a single array named result
.plus
method concatenates the two arrays, resulting in a new array that contains all the elements of both arrays.fun main(args: Array<String>) {
val arr1 = arrayOf("apple", "banana")
val arr2 = arrayOf("cherry", "date")
val result = arr1.plus(arr2)
println("Joined array: ${result.joinToString()}")
}
Joined array: apple, banana, cherry, date
In this tutorial, we learned How to Join Arrays in Kotlin language with well detailed examples.