集合
1.Set
Set 特点是内容是不能有重复
Set<元素类型> 变量名 = <元素类型>{元素1, 元素2, ...}
var uniqueNames = <String>{'Seth', 'Kathy', 'Lars'};
main(){
Set aset = new Set();
aset.add('a');
aset.add('b');
aset.add('a');
print(aset);
}
运行结果:
{a, b}
2. Queue
Queue采用先进先出策略
import 'dart:collection';
main() {
Queue aqueue = new Queue();
aqueue.addLast('a');
aqueue.addLast('b');
aqueue.addLast('c');
print(aqueue);
//输出队列
while (aqueue.isNotEmpty) {
var item=aqueue.first;
print('出队:$item');
print('剩余:$aqueue');
print('---------------');
aqueue.removeFirst();
}
}
运行结果:
{a, b, c}
出队:a
剩余:{a, b, c}
---------------
出队:b
剩余:{b, c}
---------------
出队:c
剩余:{c}
---------------
3. Map
main() {
var colors = new Map();
colors['blue'] = false;
colors['red'] = true;
print(colors);
//遍历key
for (var key in colors.keys) print(key);
//遍历value
for (var value in colors.values) print(value);
}
运行结果:
{blue: false, red: true}
blue
red
false
true
4. List
main() {
// Specifying the length creates a fixed-length list.
var onelist = new List();
onelist.add('a');
onelist.add('b');
onelist.add('a');
onelist.add('d');
print(onelist);
for (var item in onelist) {
print(item);
}
}
运行结果:
[a, b, a, d]
a
b
a
d