Scala Stack last()方法与实例

Scala Stack last()方法与实例

Scala Stack类中,利用 isEmpty() 来检查堆栈是否为空。

方法定义: def isEmpty:Boolean

返回类型。如果堆栈是空的,返回true,否则返回false。

例子 #1:

// Scala program of isEmpty() 
// method 
  
// Import Stack 
import scala.collection.mutable._
  
// Creating object 
object GfG 
{ 
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating stack
        val s1 = Stack(1, 3, 2, 7, 6, 5) 
          
        // Print the stack 
        println(s1) 
           
        // Applying isEmpty method  
        val result = s1.isEmpty
          
        // Display output 
        print("The stack is empty: " + result) 
         
    } 
} 

输出。

Stack(1, 3, 2, 7, 6, 5)
The stack is empty: false

例子#2。

// Scala program of isEmpty() 
// method 
  
// Import Stack 
import scala.collection.mutable._
  
// Creating object 
object GfG 
{ 
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating stack
        val s1 = Stack() 
          
        // Print the stack 
        println(s1) 
           
        // Applying isEmpty method  
        val result = s1.isEmpty
          
        // Display output 
        print("The stack is empty: " + result) 
         
    } 
} 

输出。

Stack()
The stack is empty: true

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程