【问题标题】:How to copy only data of list, not the reference如何只复制列表的数据,而不是参考
【发布时间】:2019-11-08 17:48:23
【问题描述】:

我正在尝试在 Dart 中创建 int 列表列表,但遇到了一些问题。

  1. 如果创建一个 int listInt = [1,2,3] 的 List 并添加到主 List(List of List of int) 中,则添加成功。 [[1,2,3]]

  2. 现在如果我清除 listInt listInt.clear() 并添加新值 listInt = [3,2,1] 然后添加再次进入主列表;它被添加,但它也会覆盖前一个。 [[3, 2, 1], [3, 2, 1]]

  3. 我想要的是[[1,2,3],[3,2,1]]

代码如下

    List<int> listInt = List();
    List<List<int>> mainListInt = List();
    listInt.add(1);
    listInt.add(2);
    listInt.add(3);
    mainListInt.add(listInt);
    print(mainListInt);
    listInt.clear();
    print(mainListInt);
    listInt.add(3);
    listInt.add(2);
    listInt.add(1);
    mainListInt.add(listInt);
    print(mainListInt);

输出

[[1, 2, 3]]
[[]]
[[3, 2, 1], [3, 2, 1]]

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您需要确保不要将子列表的引用保存到主列表,因为这会将您注意到的编辑应用到您的子列表。
    您可以使用List.of 创建一个新实例(它会创建您列表的副本,这是一个不同的参考):

    mainListInt.add(List.of(listInt));
    

    【讨论】:

      【解决方案2】:

      您可以使用listInt.toList() 创建列表的副本。

      void main(List<String> args) {
        List<int> listInt = List();
        List<List<int>> mainListInt = List();
        listInt.add(1);
        listInt.add(2);
        listInt.add(3);
        mainListInt.add(listInt.toList());
        print(mainListInt);
        listInt.clear();
        print(mainListInt);
        listInt.add(3);
        listInt.add(2);
        listInt.add(1);
        mainListInt.add(listInt.toList());
        print(mainListInt);
      }
      

      【讨论】:

      • 这是List.from的快捷方式。
      • 这是一个独立的方法。自然,任何算法都有一个实现,它在哪里实现并不重要。你是不是暗示这种方法有点矫枉过正?
      • 我不确定您在算法部分中要说什么,但对这个问题没有。您可以查看the implementation of List.toList - 它使用List.from
      • 是的...你没有回答我的问题。 Are you hinting that this method is overkill? 即使算法在`List.from()`中实现但不是直接在这个方法中。使用Iterable.toList()有什么问题?
      【解决方案3】:
      ListDestination = List.from(ListOrigin);
      

      【讨论】:

        【解决方案4】:
        // 'list1' contains [1, 2]
        var list1 = [1, 2];
        // 'list2' contains [1, 2]
        var list2 = [...list1];// here we are copying only data and note reference
            list2.add(56); // 'list2' contains [1, 2,56]
        // 'list2' contains [1, 2,56]
            print(list1); // this won't add 56 bcs List not reference to it 
            print(list2);// it will display 56
        

        【讨论】:

          【解决方案5】:

          我发现但不像它的一个解决方案是使用 List.generate() 获取列表中列表的特定编号。

              List<List<int>> mainListInt = List.generate(2, (i) => []);
              mainListInt[0].add(1);
              mainListInt[0].add(2);
              mainListInt[0].add(3);
              print(mainListInt);
              mainListInt[1].add(3);
              mainListInt[1].add(2);
              mainListInt[1].add(1);
              print(mainListInt);
          

          输出

          [[1, 2, 3], []]
          [[1, 2, 3], [3, 2, 1]]
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-07-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多