带有示例的 Scala Queue forall() 方法
forall()方法用于检查谓词是否对队列的所有元素都成立。
Method Definition: def forall(p: (A) => Boolean): Boolean
Return Type: It returns true if the predicate holds true for all the elements of the queue or else returns false.
示例 #1:
// Scala program of forall()
// method
// Import Queue
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating queues
val q1 = Queue(1, 3, 2, 7, 6, 5)
// Print the queue
println(q1)
// Applying forall method
val result = q1.forall(x => {x % 7 == 0})
// Displays output
print("All the elements are divisible by 7: " + result)
}
}
输出:
Queue(1, 3, 2, 7, 6, 5)
All the elements are divisible by 7: false
示例 #2:
// Scala program of forall()
// method
// Import Queue
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating queues
val q1 = Queue(1, 3, 7, 5)
// Print the queue
println(q1)
// Applying forall method
val result = q1.forall(x => {x % 2 != 0})
// Displays output
print("All the elements are odd: " + result)
}
}
输出:
Queue(1, 3, 7, 5)
All the elements are odd: true