在 Scala Stack class
,则contains()方法用于检查是否在not的堆栈中存在某个元素。
函数定义: def contains(elem: A): Boolean
返回类型: It returns true if the element is present in the stack or else it returns false.
范例1:
// Scala program of contains()
// method
import scala.collection.mutable.Stack
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a stack
val s1 = Stack(1, 2, 3, 4, 5)
// Print the stack
println(s1)
// Applying contains method
val result = s1.contains(10)
// Display output
println("Stack contains element 10: " + result)
}
}
输出:
Stack(1, 2, 3, 4, 5) Stack contains element 10: false
范例2:
// Scala program of contains()
// method
import scala.collection.mutable.Stack
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a stack
val s1 = Stack("C++", "Java", "Python", "Scala")
// Print the stack
println(s1)
// Applying contains method
val result = s1.contains("Scala")
// Display output
println("Stack contains element Scala: " + result)
}
}
输出:
Stack(C++, Java, Python, Scala) Stack contains element Scala: true
相关用法
- Scala Stack :+()用法及代码示例
- Scala Stack ++:()用法及代码示例
- Scala Stack map()用法及代码示例
- Scala Stack :()用法及代码示例
- Scala Stack last()用法及代码示例
- Scala Stack pop()用法及代码示例
- Scala Stack sum()用法及代码示例
- Scala Stack last()用法及代码示例
- Scala Stack take()用法及代码示例
- Scala Stack max()用法及代码示例
- Scala Stack +:()用法及代码示例
- Scala Stack top()用法及代码示例
- Scala Stack min()用法及代码示例
注:本文由纯净天空筛选整理自rupesh_rao大神的英文原创作品 Scala Stack contains() method with example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。