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?

Kotlinリストの指定した件数を取得する方法

Posted at

実装などで、最初の数件だけ表示してViewAllボタンなどが置いてあって全ては表示しないけど頭の数件表示みたいなUIの時に便利な方法です。

Kotlinのコレクションを使用することで可能です。
takeを使用します。

.kt
val list = listOf(1, 2, 3, 4, 5)
println(list.take(2)) // [1, 2]

takeは先頭から指定した数だけ要素を取得してListで返します。

他にも便利なKotlinのコレクションがあるので調べてみると面白いです。

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?