To append a list to another list in Kotlin, you can use the addAll
method or the spread operator (*
).
In this example,
list1
and list2
with initial values.addAll
method to append all elements of list2
to list1
.list1
to standard output.fun main() {
val list1 = mutableListOf(1, 2, 3)
val list2 = listOf(4, 5, 6)
list1.addAll(list2)
println("Updated list: $list1")
}
Updated list: [1, 2, 3, 4, 5, 6]
In this example,
list1
and list2
with initial values.*
) to append all elements of list2
to list1
.list1
to standard output.fun main() {
val list1 = mutableListOf(1, 2, 3)
val list2 = listOf(4, 5, 6)
list1 += list2
println("Updated list: $list1")
}
Updated list: [1, 2, 3, 4, 5, 6]
In this tutorial, we learned How to Append a List to another List in Kotlin language with well detailed examples.