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 3 years have passed since last update.

【kotlin】Set型 コレクション

0
Posted at

■Set型

前記事で紹介したList型と似ている。
複数の値を格納でき読み取り専用と可変専用がある。

■Set型の特徴

●値が重複しない
●インデックス番号で管理していない

例:Set型とList型の違い
重複した値は無視され出力される。
インデックス番号でアクセスできない。

//List型の場合
val a = listOf("あ","い","う","あ","い")
println(a)
//結果: ["あ","い","う","あ","い"] 

//Set型の場合
val b = setOf("あ","い","う","あ","い")
//値が重複しない
println(b)
//結果: ["あ","い","う"] 

printin(a[0])
//結果: あ

println(b[0])
//結果: エラー

■値の参照であれば一応可能

ループ処理を行なっているため速度が遅くなるパフォーマンスの低下につながりやすい
頻繁に使うのは避けたい。

val b = setOf("あ","い","う","あ","い")

println(b.elementAt(1))
//結果: い
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?