Dart by Example: List

  1. main() {
  2. // Specifying the length creates a fixed-length list.
  3. var list = new List(3);
  4. list[0] = 'a';
  5. list[1] = 'b';
  6. list[2] = 'c';
  7. print(list);
  8. // Leaving out the lenght creates a growable list.
  9. var growable = new List();
  10. growable.addAll(['grow', 'able']);
  11. print(growable);
  12. // Lists can be defined using bracket literals.
  13. //
  14. var list2 = ['also', 'growable'];
  15. list2.add(42);
  16. print(list2);
  17. // modifying growable lists during iteration can cause ConcurrentModificationErrors
  18. var list3 = [47, 3, 25];
  19. try {
  20. for (var item in list3) {
  21. if (item < 10) {
  22. list3.remove(item);
  23. }
  24. }
  25. } catch(e) {
  26. print('error');
  27. }
  28. }
  29.  
  1. $ dart list.dart
  2. [a, b, c]
  3. [grow, able]
  4. [also, growable, 42]
  5. error

by @jryanio | source | license