带有示例的 Scala Queue product() 方法
product() 方法用于返回队列中所有元素的乘积。
Method Definition: def product: A
Return Type: It returns the product of all the elements of the queue.
示例 #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