Jak*_*sMD 1 list deep-copy nested-lists dart flutter
如何在 dart 中创建嵌套列表的副本?在此代码中,我对副本所做的更改也会在原始代码中更改
List board = [[0,0,0], [0,0,0], [0,0,0]];
List boardCopy = List.from(board); // create copy of the board
boardCopy[0][0] = 1; // change copy
print(board); // print original board
OUTPUT:
[[1,0,0], [0,0,0], [0,0,0]] <-- it has changed the original board!!!
Run Code Online (Sandbox Code Playgroud)
我解决了:
List boardCopy = board.map((element) => List.from(element)).toList();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1178 次 |
| 最近记录: |