如何在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!!!

最佳答案

我解决了:

List boardCopy = board.map((element) => List.from(element)).toList();

关于list - 如何在Dart中深度复制嵌套列表?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/64594543/

10-12 00:43
查看更多