【问题标题】:How to deep copy nested list in dart?如何在飞镖中深度复制嵌套列表?
【发布时间】:2020-10-29 15:45:11
【问题描述】:

如何在 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 flutter dart nested-lists deep-copy


    【解决方案1】:

    我解决了:

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

    【讨论】:

    • 你将如何为地图(键值)做同样的事情?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 2019-07-09
    • 1970-01-01
    • 2022-01-24
    • 2020-10-30
    相关资源
    最近更新 更多