- Dart 程式設計教程
- Dart 程式設計 - 首頁
- Dart 程式設計 - 概述
- Dart 程式設計 - 環境
- Dart 程式設計 - 語法
- Dart 程式設計 - 資料型別
- Dart 程式設計 - 變數
- Dart 程式設計 - 運算子
- Dart 程式設計 - 迴圈
- Dart 程式設計 - 決策
- Dart 程式設計 - 數字
- Dart 程式設計 - 字串
- Dart 程式設計 - 布林值
- Dart 程式設計 - 列表
- Dart 程式設計 - 列表
- Dart 程式設計 - 對映
- Dart 程式設計 - 符號
- Dart 程式設計 - 字元符
- Dart 程式設計 - 列舉
- Dart 程式設計 - 函式
- Dart 程式設計 - 介面
- Dart 程式設計 - 類
- Dart 程式設計 - 物件
- Dart 程式設計 - 集合
- Dart 程式設計 - 泛型
- Dart 程式設計 - 包
- Dart 程式設計 - 異常
- Dart 程式設計 - 除錯
- Dart 程式設計 - 型別定義
- Dart 程式設計 - 庫
- Dart 程式設計 - 非同步
- Dart 程式設計 - 併發
- Dart 程式設計 - 單元測試
- Dart 程式設計 - HTML DOM
- Dart 程式設計有用資源
- Dart 程式設計 - 快速指南
- Dart 程式設計 - 資源
- Dart 程式設計 - 討論
Dart 程式設計 - 更新列表
更新索引
Dart 允許修改列表中專案的 值。換句話說,可以重寫列表項的值。以下示例說明了這一點:
void main() {
List l = [1, 2, 3,];
l[0] = 123;
print (l);
}
以上示例更新了索引為 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> - 表示更新值的 iterable 物件。
以下示例說明了這一點:
即時演示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}');
}
它應該產生以下輸出:
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]
dart_programming_lists_basic_operations.htm
廣告