📜  Scala List ::()运算符与示例

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

Scala List ::()运算符与示例

Scala 中的::()运算符用于将元素添加到列表的开头。

示例 #1:

// Scala program of ::()
// method
  
// Creating object
object GfG
{ 
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a list
        val m1 = List(1, 2, 3)
          
        // Applying :: operator
        val res = m1.::("5")
          
        // Displays output
        println(res)
      
    }
}
输出:
List(5, 1, 2, 3)

示例 #2:

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