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.

Dart の list[0] と list.elementAt[0] は何が違うの?

Posted at

疑問

dart には List の指定 index の要素を取得する方法として以下の 2 つがある。
両者で何が違うのか?

var list = ['a', 'b', 'c'];
print(list[0]); // 'a'
print(list.elementAt(0)); // 'a'

得られる結果は同じだが定義場所が違う

[]elementAt() も指定した index の要素を得られるという点では同じ。
ただし、[]List インタフェースで定義されているのに対して elementAt()Iterable クラスで定義されている
つまり elementAt() による要素の取得は List に限らず Iterable であれば使用可能。

使い分けとしては

  • List であることを期待するのであれば []
  • Iterable ではあればよいのであれば elementAt()

とするのが良さそう。

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?