Skip to content

Instantly share code, notes, and snippets.

@davidmigloz
Last active May 3, 2021 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidmigloz/9b94699916ec1f8b4e4a8d1d5a06a8c1 to your computer and use it in GitHub Desktop.
Save davidmigloz/9b94699916ec1f8b4e4a8d1d5a06a8c1 to your computer and use it in GitHub Desktop.
Dart vs Kotlin: lists
import 'package:collection/collection.dart';
void main() {
// Mutable growable list
final mutableList = [1, 2, 3];
mutableList.add(499);
final element = mutableList[3];
mutableList.length = 2; // truncate
// Mutable fixed-length list
final fixedLengthList = List.filled(5, 0);
fixedLengthList[0] = 87;
// Generate
final gen = List<int>.generate(5, (int i) => i * i);
// Fixed-length empty list
final fixedLengthEmpty = List<int>.empty();
// Immutable list (copy of the original)
final immutableList = List<int>.unmodifiable([1, 2, 3]);
// Immutable list view (wrap of the original)
final immutableListView = UnmodifiableListView(mutableList);
// Equality
print([1, 2, 3] == [1, 2, 3]); // false
print(ListEquality().equals([1, 2, 3], [1, 2, 3])); // true
}
fun main() {
// Mutable growable list
val mutableList = mutableListOf(1, 2, 3)
mutableList.add(499)
val element = mutableList[3]
val truncated = mutableList.take(2) // (new list)
// Mutable fixed-length array
val fixedLengthArray = Array(5) { 0 }
fixedLengthArray[0] = 87
// Generate
val generated = List(5) { it * it }
// Immutable empty list
val emptyList = emptyList<Int>()
// Immutable list
val immutableList = listOf(1, 2, 3)
// Immutable list view (wrap of the original)
val immutableListView = mutableList as List<Int>
// Equality
print(listOf(1, 2, 3) == listOf(1, 2, 3)) // true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment