在 Scala Stack class
,则forall()方法用于检查谓词是否对堆栈的所有元素都成立。
函数定义: def forall(p: (A) => Boolean): Boolean
返回类型: It returns true if the predicate holds true for all the elements of the stack or else returns false.
范例1:
// Scala program of forall()
// method
// Import Stack
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating stack
val s1 = Stack(6, 2, 3, 4, 5)
// Print the stack
println(s1)
// Applying forall method
val result = s1.forall(x => {x % 7 == 0})
// Display output
println("All the elements are divisible by 7: " + result)
}
}
输出:
Stack(6, 2, 3, 4, 5) All the elements are divisible by 7: false
范例2:
// Scala program of forall()
// method
// Import Stack
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating stack
val s1 = Stack(7, 9, 3, 1, 5)
// Print the stack
println(s1)
// Applying forall method
val result = s1.forall(x => {x % 2 == 1})
// Display output
println("All the elements are odd: " + result)
}
}
输出:
Stack(7, 9, 3, 1, 5) All the elements are odd: true
相关用法
- Scala Set forall()用法及代码示例
- Scala SortedSet forall()用法及代码示例
- Scala Queue forall()用法及代码示例
- Scala List forall()用法及代码示例
- Scala TreeSet forall()用法及代码示例
- Scala Iterator forall()用法及代码示例
- Scala Stack take()用法及代码示例
- Scala Stack sum()用法及代码示例
- Scala Stack ++:()用法及代码示例
- Scala Stack :()用法及代码示例
- Scala Stack +:()用法及代码示例
- Scala Stack :+()用法及代码示例
- Scala Stack max()用法及代码示例
注:本文由纯净天空筛选整理自rupesh_rao大神的英文原创作品 Scala Stack forall() method with example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。