facebook twitter hatena line email

「Android/kotlin/配列」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(配列プロパティ値取得)
(配列プロパティ値取得)
行22: 行22:
 
user2.name = "jiro"
 
user2.name = "jiro"
 
var users: ArrayList<User> = arrayListOf(user, user2)
 
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)
 +
}
 +
</pre>
 +
 +
==配列プロパティ条件一致で更新==
 +
<pre>
 +
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) {
 
for (user: User in users) {
 
     Log.i("test", "user.name=" + user.name)
 
     Log.i("test", "user.name=" + user.name)
 
}
 
}
var username = users.firstOrNull { it.name.contains("taro") }?.name ?: "" // taro
 
 
</pre>
 
</pre>

2020年2月21日 (金) 16:40時点における版

配列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)
}