0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

kotlin map 置き換え

Last updated at Posted at 2022-05-23

Kotlin map

役割:リストの置き換え

使える型

CharSequence

使うシーン

for文を簡易的に書く方法みたいなイメージ
リストに対して使用
※イメージです・・・。

使い方

map
A.map{ it.XXX }
A(CharSequence)11つに対して順番に処理を実行し、Aに再代入

リストの各要素に対して処理実行

map
  fun map(){
    val characterList = mutableListOf("Beckey", "Binnie", "Bonald", "Baisy", "Boofy")
    val characterListReplacedNN = characterList.map { it.replace("B","nn") }
    println(characterListReplacedNN)
  }
result
[nneckey, nninnie, nnonald, nnaisy, nnoofy]

replaceメソッドについてはこちら

文字列にmapを実行どうなるのか?

map
  fun map(){
    val characterString = "BeckyBouse".map { it }
    println(characterString)
  }
result
[B, e, c, k, y, B, o, u, s, e]

1文字ずつ分割され、リスト化される

ちょっと面白いなと思ったこと

map
  fun map(){
    val suuji = 1847090940
    println(suuji.toString().map { it.javaClass.kotlin })
    println(suuji.toString().map { (it -'0').javaClass.kotlin })
  }
result
[class kotlin.Char, class kotlin.Char, class kotlin.Char, class kotlin.Char, class kotlin.Char, class kotlin.Char, class kotlin.Char, class kotlin.Char, class kotlin.Char, class kotlin.Char]
[class kotlin.Int, class kotlin.Int, class kotlin.Int, class kotlin.Int, class kotlin.Int, class kotlin.Int, class kotlin.Int, class kotlin.Int, class kotlin.Int, class kotlin.Int]

つまり、ただmapを使うとchar型で、mapの中で-'0'をするとInt型に変換できる。
it.toIntは、コンパイルエラーになります!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?