无法将变量初始化为ArrayList.clone()

时间:2019-04-06 04:59:11

标签: java android kotlin

尝试创建ArrayList players的克隆。我想完全克隆它(ArrayList包括它的内容)。

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初始化返回此错误: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 {

}

有什么问题吗?

0 个答案:

没有答案