arrays - Scala 中数组的按元素求和

标签 arrays scala

如何计算数组的逐元素总和?

val a = new Array[Int](5)
val b = new Array[Int](5)
// assign values
// desired output: Array -> [a(0)+b(0), a(1)+b(1), a(2)+b(2), a(3)+b(3), a(4)+b(4)]

a.zip(b).flatMap(_._1+_._2)

missing parameter type for expanded function

最佳答案

尝试:

a.zip(b).map { case (x, y) => x + y }

关于arrays - Scala 中数组的按元素求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35064883/

相关文章:

c# - 将字符串 [] 转换为整数 []

java - java中如何将爬取的数据存储到sql数据库中

c - C 中的指针——加 1

尝试编译 SBT 项目时 ScalaPB 不工作。编译失败,错误代码 "object gen is not a member of package scalapb"

scala - 在Scala源中导入 'val'包

c - 对 2 组数组进行求和时遇到问题

javascript - 基于对象属性对数组进行排序 - Javascript

scala - 如何找到idea IDE中使用的隐式val或def?

scala - 在映射中使用 Enumeratum 枚举不起作用

scala - 在 Spark 中使用 Breeze