Does anyone agree that the output of this code is surprising?
If so, can anyone explain what is happening. Why does add(499) result in three new elements with value 499 instead of just one?
shared[0] has type List<int> so add performed on shared[0] should have no effect on shared[1].
// output is
[[], [], []]
[[499, 599], [499, 599], [499, 599]]
499
599
499
599
499
599
Dart program:
main()
{
var xx = <int>[];
final shared = List.filled(3, xx);
print(shared);
shared[0].add(499);
shared[1].add(599);
print(shared);
for (int k = 0; k < shared.length; ++k) {
for (int j = 0; j < shared[k].length; ++j)
print(shared[k][j]);
}
}