Scala ListSet sum() 方法与示例
在 Scala ListSet 中,sum() 方法用于添加声明的 listSet 的所有元素。
Method Definition: def sum(num: math.Numeric[B]): B
Return Type: It returns the sum of all the elements of the listSet.
示例 1:
// Scala program of sum()
// method
import scala.collection.immutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a list
val m1 = ListSet(1, 2, 3, 4, 5, 7)
// Applying sum method
val result = m1.sum
// Displays output
println(result)
}
}
输出:
22
示例 2:
// Scala program of sum()
// method
import scala.collection.immutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a list
val m1 = ListSet(1, -2)
// Applying sum method
val result = m1.sum
// Displays output
println(result)
}
}
输出:
-1