当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Scala Stack contains()用法及代码示例


在 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


相关用法


注:本文由纯净天空筛选整理自rupesh_rao大神的英文原创作品 Scala Stack contains() method with example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。