- Dart 程式設計教程
- Dart 程式設計 - 首頁
- Dart 程式設計 - 概述
- Dart 程式設計 - 環境
- Dart 程式設計 - 語法
- Dart 程式設計 - 資料型別
- Dart 程式設計 - 變數
- Dart 程式設計 - 運算子
- Dart 程式設計 - 迴圈
- Dart 程式設計 - 決策
- Dart 程式設計 - 數字
- Dart 程式設計 - 字串
- Dart 程式設計 - 布林值
- Dart 程式設計 - 列表
- Dart 程式設計 - 列表
- Dart 程式設計 - 對映
- Dart 程式設計 - 符號
- Dart 程式設計 - Rune
- Dart 程式設計 - 列舉
- Dart 程式設計 - 函式
- Dart 程式設計 - 介面
- Dart 程式設計 - 類
- Dart 程式設計 - 物件
- Dart 程式設計 - 集合
- Dart 程式設計 - 泛型
- Dart 程式設計 - 包
- Dart 程式設計 - 異常
- Dart 程式設計 - 除錯
- Dart 程式設計 - 型別定義
- Dart 程式設計 - 庫
- Dart 程式設計 - 非同步
- Dart 程式設計 - 併發
- Dart 程式設計 - 單元測試
- Dart 程式設計 - HTML DOM
- Dart 程式設計實用資源
- Dart 程式設計 - 快速指南
- Dart 程式設計 - 資源
- Dart 程式設計 - 討論
Dart 程式設計 - 集合 Set
Set 表示一個物件集合,其中每個物件只能出現一次。dart:core 庫提供了 Set 類來實現這一點。
語法
Identifier = new Set()
或
Identifier = new Set.from(Iterable)
其中,Iterable 表示要新增到 Set 的值列表。
示例
void main() {
Set numberSet = new Set();
numberSet.add(100);
numberSet.add(20);
numberSet.add(5);
numberSet.add(60);
numberSet.add(70);
print("Default implementation :${numberSet.runtimeType}");
// all elements are retrieved in the order in which they are inserted
for(var no in numberSet) {
print(no);
}
}
它應該產生以下輸出 -
100 20 5 60 70
圖示:Set.from()
void main() {
Set numberSet = new Set.from([12,13,14]);
print("Default implementation :${numberSet.runtimeType}");
// all elements are retrieved in the order in which they are inserted
for(var no in numberSet) {
print(no);
}
}
它應該產生以下輸出 -
12 13 14
高階 Dart 集合 ─ dart:collection 庫
dart:collection 庫提供了能夠實現各種 Dart 集合的類。在本節中,我們將討論以下主題。
- HashMap
- HashSet
- LinkedList
- Queue
HashMap
HashMap 是基於雜湊表的 Map 實現。當您遍歷 HashMap 的鍵或值時,您無法期望特定的順序。語法如下所示 -
語法
Identifier= new HashMap()
示例
以下示例演示瞭如何實現 HashMap -
import 'dart:collection';
main() {
var accounts = new HashMap();
accounts['dept']='HR';
accounts['name']='Tom';
accounts['email']='tom@xyz.com';
print('Map after adding entries :${accounts}');
}
它應該產生以下輸出 -
Map after adding entries :{email: tom@xyz.com, dept: HR, name: Tom}
向 HashMap 新增多個值
HashMap 類從 Map 類繼承了addAll() 函式。此函式允許一次新增多個值。
語法
HashMap.addAll(Iterable)
其中,Iterable 表示要插入的值列表。
示例
import 'dart:collection';
main() {
var accounts = new HashMap();
accounts.addAll({'dept':'HR','email':'tom@xyz.com'});
print('Map after adding entries :${accounts}');
}
它應該產生以下輸出 -
Map after adding entries :{email: tom@xyz.com, dept: HR}
從 HashMap 中移除值
remove() 和clear() 函式用於從 HashMap 中移除條目。remove() 函式傳遞一個表示要移除的條目的鍵。clear() 函式用於移除 Map 中的所有條目。
示例
import 'dart:collection';
main() {
var accounts = new HashMap();
accounts['dept'] = 'HR';
accounts['name'] = 'Tom';
accounts['email'] = 'tom@xyz.com';
print('Map after adding entries :${accounts}');
accounts.remove('dept');
print('Map after removing entry :${accounts}');
accounts.clear();
print('Map after clearing entries :${accounts}');
}
它應該產生以下輸出 -
Map after adding entries :{email: tom@xyz.com, dept: HR, name: Tom}
Map after removing entry :{email: tom@xyz.com, name: Tom}
Map after clearing entries :{}
HashSet
HashSet 是一個基於無序雜湊表的 Set 實現。語法如下 -
語法
Identifier = new HashSet()
add() 函式可用於填充 HashSet 例項。
示例
import 'dart:collection';
void main() {
Set numberSet = new HashSet();
numberSet.add(100);
numberSet.add(20);
numberSet.add(5);
numberSet.add(60);
numberSet.add(70);
print("Default implementation :${numberSet.runtimeType}");
for(var no in numberSet){
print(no);
}
}
它應該產生以下輸出 -
60 20 100 5 70
向 HashSet 新增多個值
addAll() 函式允許向 HashSet 新增多個值。以下示例說明了這一點 -
示例
import 'dart:collection';
void main() {
Set numberSet = new HashSet();
numberSet.addAll([100,200,300]);
print("Default implementation :${numberSet.runtimeType}");
for(var no in numberSet){
print(no);
}
}
它應該產生以下輸出 -
Default implementation :_HashSet 200 300 100
從 HashSet 中移除值
remove() 函式移除傳遞給它的值。clear() 函式移除 HashSet 中的所有條目。
示例
import 'dart:collection';
void main() {
Set numberSet = new HashSet();
numberSet.addAll([100,200,300]);
print("Printing hashet.. ${numberSet}");
numberSet.remove(100);
print("Printing hashet.. ${numberSet}");
numberSet.clear();
print("Printing hashet.. ${numberSet}");
}
它應該產生以下輸出 -
Printing hashet.. {200, 300, 100}
Printing hashet.. {200, 300}
Printing hashet.. {}
dart_programming_collection.htm
廣告