「Android/kotlin/配列」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「<pre> var names:Array<String> if (names.isNotEmpty()) { for (name: String in names) { Log.d("test", "name=" + name) } } </pre>」) |
|||
行1: | 行1: | ||
+ | ==配列for-in== | ||
<pre> | <pre> | ||
var names:Array<String> | var names:Array<String> | ||
行6: | 行7: | ||
} | } | ||
} | } | ||
+ | </pre> | ||
+ | |||
+ | ==配列プロパティ値取得== | ||
+ | User.kt | ||
+ | <pre> | ||
+ | class User { | ||
+ | var name: String = "" | ||
+ | } | ||
+ | </pre> | ||
+ | <pre> | ||
+ | var user = User() | ||
+ | user.name = "taro" | ||
+ | var user2 = User() | ||
+ | user2.name = "jiro" | ||
+ | var users: ArrayList<User> = arrayListOf(user, user2) | ||
+ | for (user: User in users) { | ||
+ | Log.i("test", "user.name=" + user.name) | ||
+ | } | ||
+ | var username = users.firstOrNull { it.name.contains("taro") }?.name ?: "" | ||
</pre> | </pre> |
2020年2月21日 (金) 16:37時点における版
配列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) for (user: User in users) { Log.i("test", "user.name=" + user.name) } var username = users.firstOrNull { it.name.contains("taro") }?.name ?: ""