Lists (aka Arrays)

Lists of things are a requirement of any language. Easing developers into the language, Dart sticks close to the expected with lists.

primitives/lists.dart
 
var​ muppets = [​'The Count'​, ​'Bert'​, ​'Ernie'​, ​'Snuffleupagus'​];
 
var​ primes = [1, 2, 3, 5, 7, 11];
 
// Indexed from zero
 
muppets[0]; ​// 'The Count'
 
primes.length; ​// 6

Dart does provide some nice, consistent methods for manipulating lists.

 
var​ muppets = [​'The Count'​, ​'Bert'​, ​'Ernie'​, ​'Snuffleupagus'​];
 
muppets.setRange(1, 3, [​'Kermit'​, ​'Oscar'​]);
 
// muppets => ['The Count', 'Kermit', 'Oscar', 'Snuffleupagus']
 
muppets.removeRange(1, 3);
 
// muppets => ['The Count', 'Snuffleupagus'];
 
muppets.addAll([​'Elmo'​, ​'Cookie Monster'​]);
 
// muppets => ['The Count', 'Snuffleupagus', 'Elmo', 'Cookie Monster']

There are a number of iterating methods built in as well.

primitives/list_iterators.dart
 
var​ muppets = [​'The Count'​, ​'Bert'​, ​'Ernie'​, ​'Snuffleupagus'​];
 
 
muppets.forEach((muppet) {
 
print(​"$muppet is a muppet."​);
 
});
 
// =>
 
// The Count is a muppet.
 
// Bert is a muppet.
 
// Ernie is a muppet.
 
// Snuffleupagus is a muppet.
 
muppets.any((muppet) {
 
return​ muppet.startsWith(​'S'​);
 
});
 
// true
 
muppets.every((muppet) {
 
return​ muppet.startsWith(​'S'​);
 
});
 
// false
 
muppets.where((muppet) {
 
return​ muppet.startsWith(​'S'​);
 
});
 
// ['Snuffleupagus']

Even fold comes built into Dart. We can use it to count all the letters and spaces that make up the names of our list of muppets.

 
muppets.fold(0, (memo, muppet) {
 
return​ memo + muppet.length;
 
});
 
// 31

Thankfully, there is not much that needs to be introduced for Dart lists and arrays. They are one of many things in Dart that “just work.”

Collections

The iterating methods supported by the List class are defined by Iterable. Any Dart class that behaves this way implements the interface defined by the Iterable class. That consistency throughout the language is extraordinarily liberating—if you know Iterable, you know how to manipulate maps, lists, DOM nodelists, event streams, I/O streams, and more.

There are many members of the Iterable family in Dart. Let’s take a quick look at two of them: Set and Queue.

The Set class is an Iterable in which the elements are always unique and that exposes some set operations.

primitives/set.dart
 
var​ sesame = ​new​ Set.from([​'Kermit'​, ​'Bert'​, ​'Ernie'​]);
 
var​ muppets = ​new​ Set.from([​'Piggy'​, ​'Kermit'​]);
 
// No effect b/c Ernie is already in the Set
 
sesame.add(​'Ernie'​); ​// => ['Kermit', 'Bert', 'Ernie']
 
sesame.intersection(muppets); ​// => ['Kermit']
 
sesame.containsAll(muppets); ​// => false

The Queue, which is in dart:collection instead of core, is an Iterable that can be manipulated at the beginning.

primitives/queue.dart
 
var​ muppets = ​new​ Queue.from([​'Piggy'​, ​'Rolf'​]);
 
muppets.addFirst(​'Kermit'​);
 
// muppets => ['Kermit', 'Piggy', 'Rolf']
 
muppets.removeFirst();
 
muppets.removeLast();
 
// muppets => ['Piggy']

The corollary to the existence of Queue is that regular lists cannot be manipulated at the beginning. That is, there is no shift or unshift method for List. (Technically List has insert and remove, which take index parameters, making it possible, but ugly, to manipulate the beginning of a list.)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.8.8