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