list - 如何在飞镖中深度复制嵌套列表?
问题描述
如何在 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();
推荐阅读
- javascript - Is there a way to increase specificity by adding the element with Emotion?
- javascript - Electron adding files to public folder after build fails
- python - 找不到文件目录错误。请帮助。FileNotFoundError:[Errno 2] 没有这样的文件或目录:'script.py'
- tensorflow - 使用 [image,label] = dataset.take(2) 返回两个元组而不是一个
- android - 为什么 React 不在 Android 操作系统上呈现列表
- python - 在 Keras 中获取多输出模型的类
- java - 试图找到回文数
- python - 列出并使用我自己的函数中定义的变量
- yii2 - 将 phpGrid 与 YII2 集成
- java - 杰克逊 - 马歇尔 XML 到 POJO 无法识别的字段错误