どうも、ちょげ(@chogetarou)です。
removeAt()を使ってリスト(mutbaleList)の末尾の要素を削除する方法を紹介します。
方法

removeAt()を使ってリスト(mutbaleList)の最後の要素を削除するには、lastIndexプロパティを使います。
まず、リスト(mutbaleList)からremoveAt()を呼び出します。
そして、removeAt()の引数に、リスト(mutbaleList)のlastIndexプロパティを指定します。
list.removeAt(list.lastIndex)
上記のremoveAt()は、呼び出したリストの最後の要素を削除します。
使用例
fun main() {
val list : MutableList<Int> = mutableListOf<Int>(1, 2, 3, 4, 5)
list.removeAt(list.lastIndex)
println(list)
}
出力:
[1, 2, 3, 4]
コメント