Scala List init() 方法与示例
init()方法用于查找列表中除最后一个之外的所有元素。
Method Definition: def init: List[A]
Return Type: It returns all the elements of the list except the last one.
示例 #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()