To concatenate two lists in Kotlin, you can use the plus
operator or the plus
function.
In this example,
list1
and list2
with initial values.plus
operator (+
) to concatenate list2
to list1
.fun main() {
val list1 = listOf(1, 2, 3)
val list2 = listOf(4, 5, 6)
val concatenatedList = list1 + list2
println("Concatenated list: $concatenatedList")
}
Concatenated list: [1, 2, 3, 4, 5, 6]
In this example,
list1
and list2
with initial values.plus
function to concatenate list2
to list1
.fun main() {
val list1 = listOf(1, 2, 3)
val list2 = listOf(4, 5, 6)
val concatenatedList = list1.plus(list2)
println("Concatenated list: $concatenatedList")
}
Concatenated list: [1, 2, 3, 4, 5, 6]
In this tutorial, we learned How to Concatenate Two Lists in Kotlin language with well detailed examples.