【问题标题】:How do I combine two lists in Dart?如何在 Dart 中合并两个列表?
【发布时间】:2014-03-16 14:05:52
【问题描述】:

我想知道是否有一种简单的方法可以在 dart 中连接两个列表以创建一个全新的列表对象。我找不到类似的东西:

我的清单:

list1 = [1, 2, 3]
list2 = [4, 5, 6]

我试过了:

var newList = list1 + list2;

我想要以下组合的输出:

[1, 2, 3, 4, 5, 6]

【问题讨论】:

    标签: list generics dart concatenation


    【解决方案1】:

    你可以使用:

    var newList = new List.from(list1)..addAll(list2);
    

    如果您有多个可以使用的列表:

    var newList = [list1, list2, list3].expand((x) => x).toList()
    

    从 Dart 2 开始,您现在可以使用 +:

    var newList = list1 + list2 + list3;
    

    从 Dart 2.3 开始,您可以使用扩展运算符:

    var newList = [...list1, ...list2, ...list3];
    

    【讨论】:

    • 另一种选择(允许轻松连接多个列表):[list1, list2, list3, ...].expand((x) => x).toList();
    • 请注意,+ 运算符不能用于不同类型的列表。 (在这种情况下,您会收到类似 type 'List<Widget>' is not a subtype of type 'List<Image>'. 的错误消息。)不过,展开运算符非常适合这个用例。
    • 你能检查一下这个问题并给我一些建议吗? stackoverflow.com/questions/62228799/…
    【解决方案2】:

    Alexandres 的回答是最好的,但如果你想在你的例子中使用 +,你可以使用 Darts 运算符重载:

    class MyList<T>{
      List<T> _internal = new List<T>();
      operator +(other) => new List<T>.from(_internal)..addAll(other);
      noSuchMethod(inv){
        //pass all calls to _internal
      }
    }
    

    然后:

    var newMyList = myList1 + myList2;
    

    有效:)

    【讨论】:

      【解决方案3】:

      也许更一致~

      var list = []..addAll(list1)..addAll(list2);
      

      【讨论】:

      • 老我知道,但 ..addAll() 是什么,为什么不只是一个点?
      • @Abbas.M .. 用于链接,不用双点,你必须这样做:list = []; list.addAll(list1); list.addAll(list2);在我看来,它基本上意味着调用这个函数,但忽略它返回的内容,并继续对我们正在操作的对象进行操作。
      【解决方案4】:

      现在使用 + 运算符对列表进行 supports 串联。

      例子:

      List<int> result = [0, 1, 2] + [3, 4, 5];
      

      【讨论】:

        【解决方案5】:

        如果您想合并两个列表并删除重复项,可以这样做:

        var newList = [...list1, ...list2].toSet().toList(); 
        

        【讨论】:

        • 防止重复项目的最佳解决方案。?
        【解决方案6】:

        我们可以使用addAll()方法将另一个列表的所有元素添加到现有列表中。

        使用addAll() 方法将另一个列表的所有元素添加到现有列表中。并将所有可迭代对象附加到此列表的末尾。

        将列表的长度扩展为可迭代对象的数量。如果此列表是固定长度的,则抛出 UnsupportedError

        创建列表

        listone = [1,2,3]
        listtwo = [4,5,6]
        

        组合列表

         listone.addAll(listtwo);
        

        输出:

        [1,2,3,4,5,6]
        

        【讨论】:

        • 没错,listone 将包含总和。但是 addAll 将返回 null。不能在你想要结果的地方直接使用它(比如小部件列表)
        【解决方案7】:

        在我看来,不需要创建第三个列表...

        使用这个:

        list1 = [1, 2, 3];
        list2 = [4, 5, 6];
        list1.addAll(list2);
        
        print(list1); 
        // [1, 2, 3, 4, 5, 6] // is our final result!
        

        【讨论】:

          【解决方案8】:

          addAll 是合并两个列表最常用的方式。

          但要连接列表列表,您可以使用以下三个函数中的任何一个(示例如下):

          • expand - 将 Iterable 的每个元素扩展为零个或多个元素,
          • fold - 通过迭代地将集合中的每个元素与现有值组合,将集合缩减为单个值,
          • reduce - 通过使用提供的函数迭代组合集合的元素,将集合缩减为单个值。
          void main() {
            List<int> a = [1,2,3];
            List<int> b = [4,5];
            List<int> c = [6];
            List<List<int>> abc = [a,b,c]; // list of lists: [ [1,2,3], [4,5], [6] ]
            List<int> ints = abc.expand((x) => x).toList();
            List<int> ints2 = abc.reduce((list1,list2) => list1 + list2);
            List<int> ints3 = abc.fold([], (prev, curr) => prev + curr); // initial value is []
            print(ints);  // [1,2,3,4,5,6]
            print(ints2); // [1,2,3,4,5,6]
            print(ints3); // [1,2,3,4,5,6]
          }
          

          【讨论】:

            【解决方案9】:
             list1.followedBy(list2).toList();
            

            【讨论】:

              【解决方案10】:

              来自 Dart 2.3+ 和来自 JavaScript 社区的人们:

              var mergedList = [...listX, ...listY, ...listZ].toSet(); 
              

              toSet() 将过滤并仅返回唯一项目。

              【讨论】:

                猜你喜欢
                • 2021-09-04
                • 2021-12-23
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2020-12-23
                • 2019-05-10
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多