https://kotlinlang.org logo
Title
a

Ali

06/29/2022, 11:43 AM
What is the efficient way to modify a list in kotlin? for ex. From this list
mutableListOf(listOf(1,2,3,4,5), listOf(6,7,8,9,10))
I need this modified result but keeping original list as it is.
[[1,2], [6,7,8,9,10]]
t

Thomas Urbanitsch

06/29/2022, 2:59 PM
in general getting a modified list from an existing list without changing the existing list is what you will get with all the classic Collection transformation operations: https://kotlinlang.org/docs/collection-transformations.html
👍 1
t

Tran Thang

06/30/2022, 7:05 AM
Cay u try it:
val arr = mutableListOf(1, 2, 3, 4, 5)
val arr2 = listOf(6, 7, 8, 9, 10)
val arr4 = mutableListOf(arr.filterIndexed { index, _ -> index == 0 || index == 1 }, arr2)