following from here
below provided for the test case
@Test
fun testKotlinArrayCopy() {
data class Person(val name: String, var friends: List<Person>, var isHungry: Boolean = false)
val okyFriends = listOf(
Person(name = "Billy", friends = listOf()),
)
val peopleAtBlock1 : ArrayList<Person> = arrayListOf(
Person(name = "Oky", friends = okyFriends),
)
val peopleAtBlock2 : ArrayList<Person> = ArrayList(peopleAtBlock1.toMutableList())
peopleAtBlock1[0].friends[0].isHungry = true
// here i expect variable peopleAtBlock2 doesn't changed
// because i just change the peopleAtBlock1 only
assertFalse(peopleAtBlock1[0] == peopleAtBlock2[0])
}
when i run the test it fails because the peopleAtBlock2
changed unexpectedly caused by this code
peopleAtBlock1[0].friends[0].isHungry = true
i want to keep value of peopleAtBlock2
is not changed, how do i do that?
thanks
EDIT:
i have follow answer here using
fun Array<BooleanArray>.copy() = map { it.clone() }.toTypedArray()
converted to my case, the function would be
fun ArrayList<Person>.copy2() = map { it.copy() }.toTypedArray()
but still doesn't work
please check my repl for running in cloud if you dont have kotlin