【问题标题】:How do I multiply two lists in flutter如何在颤动中将两个列表相乘
【发布时间】:2022-01-06 19:18:49
【问题描述】:

如何在颤振中将两个不同的列表相乘。比如下面的例子

列出 a = [2,3];

列表 b = [1,4];

列表 c = [3,5];

如何获得

列表 d = [6,60];

【问题讨论】:

    标签: list flutter dart


    【解决方案1】:
    void main() {
      List a = [2, 3];
    
      List b = [1, 4];
    
      List c = [3, 5];
    
      List d = [];
    
      for (int i = 0; i < a.length; i++) {
        d.add(a[i] * b[i] * c[i]);
      }
      print(d);
    }
    

    【讨论】:

      【解决方案2】:

      如果你不知道:

      • 您收到的列表数量
      • 列表中的元素数量

      你可以这样做:

      void main() {
        //creating an empty growable list of list
        List<List<int>> listOfLists = List.generate(0, (i) => []);
      
        //your N List, maybe from api or something
        List<int> a = [2, 3];
        List<int> b = [1, 4];
        List<int> c = [3, 5];
      
        //adding all list to main one
        listOfLists.addAll([a, b, c]);
      
        //creating list which will have results
        final results = [];
      
        //recursive logic
        listOfLists.asMap().forEach((listOfListsIndex, list) {
          if (listOfListsIndex == 0) {
            //adding first values as there's none to multiply
            //you can remove the if statement if you init earlier
            //final results = listOfLists[0];
            //listOfLists.removeAt(0);
            results.addAll(list);
          } else {
            list.asMap().forEach((listIndex, value) {
              if (results.length > listIndex) {
                //case when listOfLists[0] length is minor
                //preventing error
                //List<int> a = [2];
                //List<int> b = [1, 4];
                //List<int> c = [3, 5];
                //List<int> d = [3, 5, 4, 6, 7];
                results[listIndex] = results[listIndex] * value;
              } else {
                results.add(value);
              }
            });
          }
        });
      
        print(results);
        //[6, 60]
      }
      

      【讨论】:

        【解决方案3】:

        执行此操作的通用函数,其中input 是整数列表的列表,其中每个列表的长度可以是任何值。

        List<int> productList(List<List<int>> input) {
          // handle some edge cases
          if (input.isEmpty) {
            return [];
          } else if (input.length < 2) {
            return input.first;
          }
          // sort the input so the largest list is first
          input.sort(
            (listA, listB) => listB.length.compareTo(listA.length),
          );
          var product = input.first;
          for (var productIndex = 0; productIndex < product.length; productIndex++) {
            // iterate over the rest of the list, keep multiplying if each list
            // contains a number at productIndex
            for (var inputIndex = 1; inputIndex < input.length; inputIndex++) {
              var numList = input[inputIndex];
              if (numList.length > productIndex) {
                product[productIndex] *= numList[productIndex];
              } else {
                break;
              }
            }
          }
          return product;
        }
        

        在你的例子中:

        var a = [2,3];
        var b = [1,4];
        var c = [3,5];
        var input = [a, b, c];
        print(productList(input));
        

        产量:

        [6, 60]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多