Try to search your question here, if you can't find : Ask Any Question Now ?

Can't initialise variable as ArrayList.clone()

HomeCategory: stackoverflowCan't initialise variable as ArrayList.clone()
Avatargaurav asked 5 months ago

Trying to create a clone of ArrayList players. I want to clone it entirely (the ArrayList including its contents).

Activity.kt

lateinit var playersinfo: PlayersInfo
lateinit var players: ArrayList<Player>
lateinit var tempPlayers: ArrayList<Player>

override fun onCreate(savedInstanceState: Bundle?) {

val intent = intent
playersinfo = intent.getParcelableExtra<PlayersInfo>("playersinfo")
players = playersinfo.players
var tempPlayers: ArrayList<Player> = players.clone() // RETURNS ERROR
players.forEach { tempPlayers.add(it)}

tempPlayers initialisation returns this error: enter image description here

Player.kt

@Parcelize
data class Player (
    val name: String,
    val age: Int,
    val gender: String
): Parcelable

@Parcelize
class PlayersInfo(
    val players: ArrayList<@RawValue Player>, val anyBelow18: Boolean = players.any { player -> player.age < 18 },
    var location: String = "Other"
): Parcelable {

}

Any idea what the problem is?

1 Answers
Best Answer
AvatarJyoti answered 5 months ago
Your Answer

17 + 2 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com