代码之家  ›  专栏  ›  技术社区  ›  Jordan Davies

如何在Dart中合并两个列表?

  •  1
  • Jordan Davies  · 技术社区  · 7 年前

    我正在尝试合并2个列表,因此输出如下:

    List<String> array1 = ["John", "Bob", "Fred", "June", "Tom"];
    List<String> array2 = ["House", "Flat", "Bungalow"];
    
    List<String> output = //["John", "House", "Bob", "Flat", "Fred", "Bungalow", "June", "Tom"]
    

    我怎样才能做到这一点?我已经看了这本书 zip

    :为了澄清,我基本上想做与zip函数相同的事情,但是如果一个数组比另一个数组长,我只想将剩余的项附加到末尾。

    4 回复  |  直到 7 年前
        1
  •  8
  •   Aly Yasser Pranajaya    6 年前

    我认为这也应该奏效

    void main() {
      var list1 = ["John", "Bob", "Fred", "June", "Tom"];
      var list2 = ["House", "Flat", "Bungalow"];
      var list3 = [...list1, ...list2];
    
      print(list1);
      print(list2);
      print(list3);
    }
    

    输出应如下所示(在中测试 DartPad )

    [John, Bob, Fred, June, Tom]
    [House, Flat, Bungalow]
    [John, Bob, Fred, June, Tom, House, Flat, Bungalow]
    
        2
  •  3
  •   Günter Zöchbauer    7 年前

    我不知道任何可以使用的东西,但这应该可以满足您的需要:

    import 'dart:math' as math;
    
    void main() {
      List<String> array1 = ["John", "Bob", "Fred", "June", "Tom"];
      List<String> array2 = ["House", "Flat", "Bungalow"];
    
      List<String> output = List<String>(array1.length + array2.length);
      int i = 0;
      for (i; i < math.min(array1.length, array2.length); i++) {
        output[i * 2] = array1[i];
        output[i * 2 + 1] = array2[i];
      }
      print(output);
      if (array1.length != array2.length) {
        if (array1.length > array2.length) {
          output.setRange(i * 2, output.length, array1.sublist(i));
        } else {
          output.setRange(i * 2, output.length, array2.sublist(i));
        }
      }
      print(output);
    }
    
        3
  •  3
  •   SpencerPark    7 年前

    另一个解决方案是下面的解决方案,它的效率可能不如Gunter的解决方案,但在本质上更具功能性,我喜欢从多个角度来解决问题。如果数组像这些一样短,那么连接上的优化可能无论如何都不是问题。

    import 'dart:math' as math;
    
    main() {
      List<String> array1 = ["John", "Bob", "Fred", "June", "Tom"];
      List<String> array2 = ["House", "Flat", "Bungalow"];
    
      //["John", "House", "Bob", "Flat", "Fred", "Bungalow", "June", "Tom"]
      List<String> output =
          Iterable.generate(math.max(array1.length, array2.length))
              .expand((i) sync* {
        if (i < array1.length) yield array1[i];
        if (i < array2.length) yield array2[i];
      }).toList();
    
      print(output);
    }
    
        4
  •  2
  •   pbn    7 年前

    merge 功能:

    import 'dart:math';
    
    List<String> merge(List<String> a, List<String> b) {
        List<String> output = [];
    
        var min_length = min(a.length, b.length);
        var max_length = max(a.length, b.length);
    
        for(var i = 0; i < min_length; i++) {
          output.add(a[i]);
          output.add(b[i]);
        }
    
        List<String> longer = a.length > b.length ? a : b;
    
        for(var i = min_length; i < max_length; i++) {
          output.add(longer[i]);
        }
    
        return output;
      }
    
    void main() {
        merge(
          ["John", "Bob", "Fred", "June", "Tom"],
          ["House", "Flat", "Bungalow"]
        ).forEach((e) => print(e));
    
        print('------');
    
        merge(
          ["John", "Bob"],
          ["House", "Flat", "Bungalow"]
        ).forEach((e) => print(e));
    }
    
        5
  •  2
  •   Chris Joseph    6 年前

    你可以用 addAll()

    List<int> a =[1,2];  List<int> b =[1,2];
    List<int> c =[];
    c.addAll(a);
    c.addAll(b);
    

    或 insertAll(index,Iterateable<>)

    List<int> a =[1,2];  
    List<int> b =[1,2];
    a.insertAll(a.length,b);
    
       
    
        6
  •  2
  •   Dharman vijay    5 年前

    我也找不到解决方案,如果可能的话,我不想使用列表转换或数学函数,所以我写了以下内容:

    extension IterableExtensions<T> on Iterable<T> {
      Iterable<T> merge(Iterable<T> other) sync* {
        
        Iterator<T> iter1 = this.iterator;
        Iterator<T> iter2 = other.iterator;
    
        bool has1 = iter1.moveNext();
        bool has2 = iter2.moveNext();
        while(has1 && has2) {
          yield iter1.current;
          yield iter2.current;
          has1 = iter1.moveNext();
          has2 = iter2.moveNext();
        }
        while(has1) {
          yield iter1.current;
          has1 = iter1.moveNext();
        }
        while(has2) {
          yield iter2.current;
          has2 = iter2.moveNext();
        }
      }
    }
    

    也许可以稍微整理一下,但现在可以用了。

    例子 here .

        7
  •  0
  •   Cristiana Pereira    5 年前

    void main() {
      List<String> myList = ['a', 'b'];
      List<String> myList2 = ['a2', 'b2'];
      List<String> myList3 = [myList, myList2].expand((x) => x).toList();
      print(myList);  // [a, b]
      print(myList2); // [a, b]
      print(myList3); // [a, b, a2, b2]
    }