Dart 基礎(chǔ)語法-集合

集合

1.Set

Set 特點是內(nèi)容是不能有重復(fù)
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);
}

運行結(jié)果:

{a, b}

2. Queue

Queue采用先進(jìn)先出策略

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();
  }
}

運行結(jié)果:


{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);
}

運行結(jié)果:


{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);
  }
}

運行結(jié)果:

[a, b, a, d]
a
b
a
d
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容