Quantcast
Channel: 配列タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 757

[Kotlin] 指定したプロパティが一致するオブジェクトを配列から削除したい

$
0
0
備忘録代わりのTipsです。 やりたいこと data class Student( var id: Int, var name: String, var age: Int, ) var studentList: MutableList<Student> = mutableListOf() for (i in 1..10) { studentList.add( studentList( id = i, name = "hoge", age = "18", ) ) } こんな雑なdata classの配列があったとして、指定したidのStudentをremoveしたい。 やり方 fun removeStudent(id: Int) { studentList.find { it.id == id }?.run { studentList -= this } } 書いていて思いましたが、よくあるequalsメソッドのoverrideみたいな感じですね。 containsでは中身の一致を確認してくれないので自作するやつです。

Viewing all articles
Browse latest Browse all 757

Trending Articles