Dart 基础语法-集合

集合

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容