i'm playing a little bit with Kotlin and i don't understand this so, maybe someone can explain it to me.
I have a data class which is:
data class Person (var name : String, var age : Int)
And i want to create a List which contains the Person with the multiplied age multiplied per 2, only on even items. So i created a new List and create a mutableList from it because i want to change their age. I did it in the following way:
val personA = Person("john", 25)
val personB = Person("Ane", 10)
val personC = Person("will", 20)
val personList = listOf(personA, personB, personC)
var mutablePersons = mutableListOf<Person>()
mutablePersons.addAll(personList)
And now i'm trying to create a new list with the items from mutablePersons modified. So i already did the following:
val mutablePersonsAgeChanged = mutablePersons.mapIndexed { index, person ->
if (index.rem(2) == 0) {
person.age = person.age * 2
}
}
And.. there is something i'm not doing well, because mutablePersonAgeChanged is List<Any> and i want a List<Person>.
So, my question is. What im doing wrong? How can make this mapIndexed Returns List<Person>