Scala Queue product()方法及示例
product()方法是用来返回队列中所有元素的乘积的。
方法定义: def product:A
返回类型。它返回队列中所有元素的乘积。
例子 #1:
// Scala program of product()
// method
// Import Queue
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a queue
val q1 = Queue(1, 2, 3, 4)
// Print the queue
println(q1)
// Applying product method
val result = q1.product
// Display output
print("Product of all the elements of the queue: " + result)
}
}
输出。
Queue(1, 2, 3, 4)
Product of all the elements of the queue: 24
例子#2。
// Scala program of product()
// method
// Import Queue
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a queue
val q1 = Queue(5, 2, 3, 7)
// Print the queue
println(q1)
// Applying product method
val result = q1.product
// Display output
print("5 * 2 * 3 * 7 = " + result)
}
}
输出。
Queue(5, 2, 3, 7)
5 * 2 * 3 * 7 = 210
极客教程