• 深拷贝


    https://stackoverflow.com/questions/21744480/clone-a-list-map-or-set-in-dart

    2

    For lists and sets, I typically use

    List<String> clone = []..addAll(originalList);
    

    The caveat, as @kzhdev mentions, is that addAll() and from()

    [Do] not really make a clone. They add a reference in the new Map/List/Set.

    That's usually ok with me, but I would keep it in mind.

    =======================================================================================================

    This solution should work:

    List list1 = [1,2,3,4];

    List list2 = list1.map((element)=>element).toList();

    It's for a list but should work the same for a map etc, remember to add to list if its a list at the end

     +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    LIst 多层情况下的addall, map表现, (无效); 还是循环到最底层生效了...

    import 'dart:io';
    import 'dart:convert';
    import 'dart:math';

    main(){
    List startList = [];
    startList.addAll(spareParts[Random().nextInt(7)]);

    print(startList);
    startList[0][0] = 999;
    print(';;;;;;;;;;;;;;;');
    print('startList $startList');
    print('spareParts $spareParts');

    List ddd = startList.map((v)=>v).toList();
    print('ddd: $ddd');
    ddd[0][0] = 88888;
    print('mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm');
    print('ddd: $ddd');
    print('startList $startList');
    print('spareParts $spareParts');
    print('mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm');
    ddd = [[],[],[],[]];
    for(int y=0; y<4; y++){
    for(int x=0; x<4; x++){
    ddd[y].add(startList[y][x]);
    }
    }

    print('ddd$ddd');
    print('startList $startList');
    print('mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm');
    ddd[0][0] = 555555555;


    print('ddd$ddd');
    print('startList $startList');
    print('mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm');
    }


    List spareParts = [
    [[0,1,0,0],
    [0,1,0,0],
    [0,1,0,0],
    [0,1,0,0]],

    [[0,0,0,0],
    [0,1,0,0],
    [1,1,1,0],
    [0,0,0,0]],

    [[0,0,0,0],
    [1,1,1,0],
    [0,0,1,0],
    [0,0,0,0]],

    [[0,0,0,0],
    [0,1,1,1],
    [0,1,0,0],
    [0,0,0,0]],

    [[0,0,0,0],
    [1,1,0,0],
    [0,1,1,0],
    [0,0,0,0]],

    [[0,0,0,0],
    [0,0,1,1],
    [0,1,1,0],
    [0,0,0,0]],

    [[0,0,0,0],
    [0,1,1,0],
    [0,1,1,0],
    [0,0,0,0]],
    ];


    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  • 相关阅读:
    7.$a = 'abcdef'; 请取出$a的值并打印出第一个字母
    8.PHP可以和sql server/oracle等数据库连接吗?
    6.能够使HTML和PHP分离开使用的模板
    4.用PHP打印出前一天的时间格式是2006-5-10 22:21:21
    5.echo(),print(),print_r()的区别
    3.数据库中的事务是什么?
    spring中配置quartz调用两次及项目日志log4j不能每天生成日志解决方法
    tomcat7性能调优与配置(以windows版为例)
    eclipse中maven下载不了私服上面的第三方包问题
    birt4.6部署到tomcat及启动服务报错解决方法
  • 原文地址:https://www.cnblogs.com/pythonClub/p/10853773.html
Copyright © 2020-2023  润新知