Rai*_*baz 2 closures for-loop dart
我在Dart中有一个网格,实现如下:
class Cell {
int row;
int col;
Cell(this.row, this.col);
}
class Grid {
List<List<Cell>> rows = new List(GRID_SIZE);
Grid() {
rows.fillRange(0, rows.length, new List(GRID_SIZE));
}
}
Run Code Online (Sandbox Code Playgroud)
而且我似乎找不到一种用适当的row和col值初始化每个单元格的方法:我尝试了两个嵌套的for循环,就像这样
for(int i = 0; i < GRID_SIZE; i++) {
for(int j = 0; j < GRID_SIZE; j++) {
rows[i][j] = new Cell(i, j);
}
}
Run Code Online (Sandbox Code Playgroud)
但由于飞镖的闭合差保护描述在这里,我的网格结束了其细胞被填充GRID_SIZE - 1的row成员。
那么,Dart中惯用的初始化嵌套列表的方式是什么?
我想这就是你想要的:
class Grid {
List<List<Cell>> rows; // = new List(GRID_SIZE);
Grid() {
rows = new List.generate(GRID_SIZE, (i) =>
new List.generate(GRID_SIZE, (j) => new Cell(i, j)));
}
}
Run Code Online (Sandbox Code Playgroud)
另请参见Dart:列表列表