Scala List ::()运算符与示例
Scala 中的::()运算符用于将元素添加到列表的开头。
Method Definition: def ::(x: A): List[A]
Return Type: It returns the stated list after adding an element to the beginning of it.
示例 #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)