gpt4 book ai didi

list - 如何在Dart中深度复制嵌套列表?

转载 作者:行者123 更新时间:2023-12-03 04:33:06 33 4
gpt4 key购买 nike

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

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com