📜  Dart编程-集合

📅  最后修改于: 2020-11-05 04:22:38             🧑  作者: Mango


与其他编程语言不同,Dart不支持数组。 Dart集合可用于复制数组等数据结构。 dart:core库和其他类在Dart脚本中启用Collection支持。

Dart集合基本上可以归类为-

Sr.No Dart collection & Description
1 List

A List is simply an ordered group of objects. The dart:core library provides the List class that enables creation and manipulation of lists.

  • Fixed Length List − The list’s length cannot change at run-time.

  • Growable List − The list’s length can change at run-time.

2 Set

Set represents a collection of objects in which each object can occur only once. The dart:core library provides the Set class to implement the same.

3 Maps

The Map object is a simple key/value pair. Keys and values in a map may be of any type. A Map is a dynamic collection. In other words, Maps can grow and shrink at runtime. The Map class in the dart:core library provides support for the same.

4 Queue

A Queue is a collection that can be manipulated at both ends. Queues are useful when you want to build a first-in, first-out collection. Simply put, a queue inserts data from one end and deletes from another end. The values are removed / read in the order of their insertion.

迭代集合

dart:core库中的Iterator类可轻松进行集合遍历。每个集合都有一个迭代器属性。此属性返回一个指向集合中对象的迭代器。

以下示例说明了使用迭代器对象遍历集合。

import 'dart:collection'; 
void main() { 
   Queue numQ = new Queue(); 
   numQ.addAll([100,200,300]);  
   Iterator i= numQ.iterator; 
   
   while(i.moveNext()) { 
      print(i.current); 
   } 
}

moveNext()函数返回一个布尔值,指示是否存在后续条目。迭代器对象的当前属性返回迭代器当前指向的对象的值。

该程序应产生以下输出

100 
200 
300