Question from the Kotlin test

How to add a new element to a list of Kotlin data class

Hard

Considering :

open class Animal
data class Cat(val name : String = "Kitty") : Animal()

The following code :

val cats: MutableList<Cat> = mutableListOf(Cat("Isidor"), Cat("Garfield"), Cat("Rogue"))
val animals: List<Animal> = cats
cats.add(Cat())
animals.forEach({ print(it) })
Author: W3D TeamStatus: PublishedQuestion passed 484 times
Edit
2
Community Evaluations
developer avatar
Raymond
17/08/2023
Intéressant comme question sans IDE. Cat() sans paramètre , sa variable name est "Milou" par défaut. Comme animals est une référence à cats. Est-ce toute modification à cats affecte animals ? Oui