📜  Scala List init() 方法与示例

📅  最后修改于: 2022-05-13 01:55:29.683000             🧑  作者: Mango

Scala List init() 方法与示例

init()方法用于查找列表中除最后一个之外的所有元素。

示例 #1:

// Scala program of init()
// method
  
// Creating object
object GfG
{ 
  
    // Main method
    def main(args:Array[String])
    {
        // Creating a list
        val m1 = List(3, 6, 2, 9, 21)
          
        // Applying init method
        val result = m1.init
          
        // Displays output
        println(result)
      
    }
} 
输出:
List(3, 6, 2, 9)

示例 #2:

// Scala program of init()
// method
  
// Creating object
object GfG
{ 
  
    // Main method
    def main(args:Array[String])
    {
        // Creating a list
        val m1 = List(3)
          
        // Applying init method
        val result = m1.init
          
        // Displays output
        println(result)
      
    }
} 
输出:
List()