「Android/kotlin/配列」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「<pre> var names:Array<String> if (names.isNotEmpty()) { for (name: String in names) { Log.d("test", "name=" + name) } } </pre>」) |
(→foreach) |
||
(同じ利用者による、間の14版が非表示) | |||
行1: | 行1: | ||
+ | ==配列== | ||
+ | 自分はforeachはbreakなどが使えないので、for-inのほうが好き | ||
+ | ===for-in=== | ||
<pre> | <pre> | ||
− | + | val names:ArrayList<String> = arrayListOf("taro", "jiro") | |
if (names.isNotEmpty()) { | if (names.isNotEmpty()) { | ||
for (name: String in names) { | for (name: String in names) { | ||
− | Log.d("test", "name= | + | Log.d("test", "name=$name") |
} | } | ||
} | } | ||
</pre> | </pre> | ||
+ | |||
+ | or | ||
+ | |||
+ | ===foreach=== | ||
+ | <pre> | ||
+ | names.forEach { name -> | ||
+ | Log.d("test", "name=$name") | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | itを使う場合 | ||
+ | <pre> | ||
+ | names.forEach { | ||
+ | Log.d("test", "name=$it") | ||
+ | } | ||
+ | </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) | ||
+ | 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) { | ||
+ | Log.i("test", "user.name=" + user.name) | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | ==mutableリストに文字列追加== | ||
+ | val names = mutableListOf<String>() | ||
+ | for (i in 1..10000) names.add("name" + i) | ||
+ | |||
+ | ==配列をnullでいれて、空の配列用意== | ||
+ | private var names: ArrayList<string?>? = null | ||
+ | names = ArrayList() | ||
+ | names!!.add("taro") // データ挿入 |
2021年2月10日 (水) 18:01時点における最新版
目次
配列
自分はforeachはbreakなどが使えないので、for-inのほうが好き
for-in
val names:ArrayList<String> = arrayListOf("taro", "jiro") if (names.isNotEmpty()) { for (name: String in names) { Log.d("test", "name=$name") } }
or
foreach
names.forEach { name -> Log.d("test", "name=$name") }
itを使う場合
names.forEach { Log.d("test", "name=$it") }
配列プロパティ値取得
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)
配列をnullでいれて、空の配列用意
private var names: ArrayList<string?>? = null names = ArrayList() names!!.add("taro") // データ挿入