Scala Set init() 方法与示例
init()方法用于查找集合中除最后一个之外的所有元素。
Method Definition: def init: Set[A]
Return Type: It returns all the elements of the set except the last one.
示例 #1:
// Scala program of init()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a set
val s1 = Set(1, 2, 3, 4, 5)
// Applying init method
val result = s1.init
// Display output
println(result)
}
}
输出:
Set(5, 1, 2, 3)
示例 #2:
// Scala program of init()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a set
val s1 = Set(16, 22, 3, 41, 51)
// Applying init method
val result = s1.init
// Display output
println(result)
}
}
输出:
Set(41, 22, 3, 16)