Dart-programming-updating-a-list

提供:Dev Guides
移動先:案内検索

Dartプログラミング-リストの更新

インデックスの更新

Dartでは、リスト内のアイテムの値を変更できます。 つまり、リスト項目の値を書き換えることができます。 次の*例*は同じことを示しています-

void main() {
   List l = [1, 2, 3,];
   1[0] = 123;
   print (1);
}

上記の例は、リストアイテムの値をインデックス0で更新します。 コードの出力は次のようになります-

[123, 2, 3]

List.replaceRange()関数を使用する

dart:coreライブラリのListクラスは、リストアイテムを変更する* replaceRange()*関数を提供します。 この関数は、指定された範囲内の要素の値を置き換えます。

List.replaceRange()関数を使用するための構文は以下のとおりです-

List.replaceRange(int start_index,int end_index,Iterable <items>)

どこで、

  • Start_index -置換を開始するインデックス位置を表す整数。
  • End_index -置換を停止するインデックス位置を表す整数。
  • <items> -更新された値を表す反復可能なオブジェクト。

次の*例*は同じことを示しています-

void main() {
   List l = [1, 2, 3,4,5,6,7,8,9];
   print('The value of list before replacing ${l}');

   l.replaceRange(0,3,[11,23,24]);
   print('The value of list after replacing the items between the range [0-3] is ${l}');
}

次の output が生成されるはずです-

The value of list before replacing [1, 2, 3, 4, 5, 6, 7, 8, 9]
The value of list after replacing the items between the range [0-3] is [11, 23, 24, 4, 5, 6, 7, 8, 9]