DMarlon Studio

Lesson 10 · 10 of 20

Working with lists · Dart

Change lists in Dart: add with add, addAll and insert, take out with remove, removeAt and removeLast, order with sort and copy parts with sublist.

Dart · Working with lists in DMarlon Studio: the example's code and its output in the terminal

Dart lists can grow and shrink. `add()` puts one element at the end and `addAll()` puts several at once, here Eva and Fran. `insert(0, 'Zoe')` puts it at a given position, the first, and everyone else moves along.

There are three ways to take things out: `remove('Ben')` looks for the value and deletes it, `removeAt(0)` deletes the given position and `removeLast()` the last one. removeAt and removeLast return what they removed, which is why served holds Zoe, the first to be served.

`sort()` orders the list itself, from lowest to highest; it does not create a new one. `reversed` gives the elements backwards, but it is not a list, just a sequence to go through; `toList()` turns it into a list to keep in ranking.

`sublist(start, end)` copies part of the list: it includes the start but not the end, so ranking.sublist(0, 3) is positions 0, 1 and 2, the three best scores. Without the second number, sublist(3) goes to the end.

`clear()` empties the list: the queue closes with [] and length 0.

Code · main.dart

void main() {  var queue = ['Ana', 'Ben', 'Carla'];  // add puts one element at the end; addAll puts several.  queue.add('Dani');  queue.addAll(['Eva', 'Fran']);  print('Queue: $queue');  // insert puts it at a given position.  queue.insert(0, 'Zoe');  print('Zoe goes first: $queue');  // remove by value, removeAt by position, removeLast the last one.  queue.remove('Ben');  String served = queue.removeAt(0);  queue.removeLast();  print('$served served, Ben left, Fran gave up: $queue');  // sort orders the list itself, from lowest to highest.  var scores = [72, 95, 58, 81, 64, 88];  scores.sort();  print('Sorted: $scores');  // reversed gives the elements backwards; toList makes a list.  var ranking = scores.reversed.toList();  print('Ranking: $ranking');  // sublist(start, end) copies a part; end is not included.  print('Top 3: ${ranking.sublist(0, 3)}');  print('From the 4th on: ${ranking.sublist(3)}');  queue.clear(); // clear empties the list  print('Queue after closing: $queue (${queue.length})');}

Output · dart run main.dart

Queue: [Ana, Ben, Carla, Dani, Eva, Fran]
Zoe goes first: [Zoe, Ana, Ben, Carla, Dani, Eva, Fran]
Zoe served, Ben left, Fran gave up: [Ana, Carla, Dani, Eva]
Sorted: [58, 64, 72, 81, 88, 95]
Ranking: [95, 88, 81, 72, 64, 58]
Top 3: [95, 88, 81]
From the 4th on: [72, 64, 58]
Queue after closing: [] (0)

Key points

  • add, addAll and insert add elements.
  • remove deletes by value; removeAt by position.
  • sort() orders the list itself.
  • reversed.toList() gives a reversed copy.
  • sublist(start, end) leaves out the end.

Exercise

Start from a list of five tasks, add one at the beginning and one at the end, remove the third, sort it alphabetically and print only the first three with sublist.