Scala 设置 last() 方法与示例
last()方法用于返回集合的最后一个元素。
Method Definition: def last: A
Return Type: It returns the last element of the set.
示例 #1:
// Scala program of last()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a set
val s1 = Set(1, 2, 3, 4)
// Applying last method
val result = s1.last
// Display output
println(result)
}
}
输出:
4
示例 #2:
// Scala program of last()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a set
val s1 = Set("geeks", "for", "geeks", "classes")
// Applying last method
val result = s1.last
// Display output
println(result)
}
}
输出:
classes