Skip to content

Instantly share code, notes, and snippets.

@kirkdrichardson
Created October 12, 2019 23:23
Show Gist options
  • Save kirkdrichardson/a4dd3a27e5750a029de689391ae90004 to your computer and use it in GitHub Desktop.
Save kirkdrichardson/a4dd3a27e5750a029de689391ae90004 to your computer and use it in GitHub Desktop.
Basic Data Structures in Dart
// Basic Data Structures in Dart (as of v2.5)
// See live at https://dartpad.dartlang.org/a4dd3a27e5750a029de689391ae90004
void main() {
print('------------------FIXED LENGTH LIST------------------------');
List<int> l = List(3);
print(l.length);
print(l);
// Careful! - Most methods (e.g. everything below) are for dyanmic lists only
// l.add(13);
// l.clear();
// l.removeLast();
// l.addAll([1,2,3]);
// Add, update, and delete by using the index
for (int i = 0; i < l.length; i++) l[i] = (i+1)*(i+1);
print(l);
print('Value of 4 is at index ${l.indexOf(4)}');
print('\n------------------DYNAMIC LIST------------------------');
// Just omit a length argument to the List constructor to create a dynamic list.
List<int> dl = List();
print(dl.length);
print(dl);
// Careful - the array index can be used to update/delete, but not to add, unless that memory space exists, e.g.
// dl[0] = 42; // Expect error
dl.add(43);
dl[0] = 42; // This doesn not throw an error bc now our list is big enough.
dl.add(43);
dl.add(44);
print(dl);
// Many more methods are available with a dynamic list
dl.removeLast();
dl.addAll([99, 100, 101]);
print(dl);
print('\n------------------SET------------------------');
// Since this is a HashMap based implementation, there are no indices.
// This way the Set interface can check for duplicates before adding in constant rather than linear time.
Set<int> s = Set();
s.add(1);
s.add(2);
bool added = s.add(3);
print('Unique value was added: $added');
s.add(3);
print(s);
added = s.add(3);
print('Non-unique value was added: $added');
print('');
Set<String> s2 = Set.from(['Hello', 'Privet', 'Hola']); // Nice static method
if (!s2.isEmpty && !s2.contains('Gamarjoba')) s2.add('Gamarjoba');
bool removed = s2.remove('Hello');
print('Item was successfully removed: $removed');
s2.forEach((String greeting) => print("$greeting! 👋")); // Iterable
s2.clear();
print(s2);
print('\n------------------MAP------------------------');
Map<String, int> grades = Map();
grades['A'] = 5;
grades['B'] = 7;
grades['C'] = 3;
grades['D'] = 1;
grades['F'] = 2;
int totalStudents = grades.values.reduce((int total, int cnt) => total + cnt);
Set<String> possibleGrades = grades.keys.toSet();
print('There are $totalStudents students in the class');
print('There are ${possibleGrades.length} possible grades');
int failingStudentCount = grades['D'] + grades['F'];
print("""
Passing student count: ${totalStudents - failingStudentCount}
Failing student count: $failingStudentCount
""");
// Can also create a map using a literal
Map<String, int> ages = {
'Suzy': 21,
'Ahmed': 24,
'Drake': 20
};
ages.addAll({'Tara': 23, 'Tristan': 22});
print(ages);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment