Android/kotlin/配列
提供: 初心者エンジニアの簡易メモ
配列for-in
var names:Array<String> if (names.isNotEmpty()) { for (name: String in names) { Log.d("test", "name=" + name) } }
配列プロパティ値取得
User.kt
class User { var name: String = "" }
var user = User() user.name = "taro" var user2 = User() user2.name = "jiro" var users: ArrayList<User> = arrayListOf(user, user2) var username = users.firstOrNull { it.name.contains("taro") }?.name ?: "" // taro for (user: User in users) { Log.i("test", "user.name=" + user.name) }
配列プロパティ条件一致で更新
var user = User() user.name = "taro" var user2 = User() user2.name = "jiro" var users: ArrayList<User> = arrayListOf(user, user2) users.firstOrNull {it.name == "taro"}?.name = "saburo" // taroをsaburoへ for (user: User in users) { Log.i("test", "user.name=" + user.name) }
mutableリストに文字列追加
val names = mutableListOf<String>() for (i in 1..10000) names.add("name" + i)