Scala List sum() 方法与示例
sum()方法用于添加所述列表的所有元素。
Method Definition: def sum(num: math.Numeric[B]): B
Return Type: It returns the sum of all the elements of the list.
示例 #1:
// Scala program of sum()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a list
val m1 = List(1, 2, 3, 4, 5, 7)
// Applying sum method
val result = m1.sum
// Displays output
println(result)
}
}
输出:
22
示例 #2:
// Scala program of sum()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a list
val m1 = List(1, 0)
// Applying sum method
val result = m1.sum
// Displays output
println(result)
}
}
输出:
1