To join lists in Kotlin, you can use the `plus` operator or the `addAll` method to combine multiple lists into a single list.
In this example,
list1
and list2
with integer values.plus
operator to join list1
and list2
into a new list named joinedList
.fun main(args: Array<String>) {
val list1 = listOf(1, 2, 3)
val list2 = listOf(4, 5, 6)
val joinedList = list1 + list2
println("Joined List: $joinedList")
}
Joined List: [1, 2, 3, 4, 5, 6]
In this example,
listA
, listB
, and listC
with string values.resultList
and use the addAll
method to add all elements from listA
, listB
, and listC
to resultList
.fun main(args: Array<String>) {
val listA = listOf("apple", "banana")
val listB = listOf("cherry", "date")
val listC = listOf("fig", "grape")
val resultList = mutableListOf<String>()
resultList.addAll(listA)
resultList.addAll(listB)
resultList.addAll(listC)
println("Joined List: $resultList")
}
Joined List: [apple, banana, cherry, date, fig, grape]
In this tutorial, we learned How to Join Lists in Kotlin language with well detailed examples.