Scala可变SortedSet的toBuffer()方法
在Scala可变集合中,SortedSet的 toBuffer() 方法用于返回一个缓冲区,其中包含SortedSet的所有元素。
方法定义:def toBuffer[B >: A]:Buffer[B]
返回类型:返回包含SortedSet所有元素的缓冲区。
示例 #1:
// toBuffer()方法的Scala程序
// 导入toBuffer()方法所需的mutable SortedSet
import scala.collection.mutable.SortedSet
// 创建对象
object GfG
{
// 主方法
def main(args:Array[String])
{
// 创建SortedSet
val s1 = SortedSet(1, 2, 3, 4, 5)
// 应用toBuffer()方法
val result = s1.toBuffer
// 显示输出
for (elem <- result)
println(elem)
}
}
1
2
3
4
5
示例 #2:
// toBuffer()方法的Scala程序
// 导入toBuffer()方法所需的mutable SortedSet
import scala.collection.mutable.SortedSet
// 创建对象
object GfG
{
// 主方法
def main(args:Array[String])
{
// 创建SortedSet
val s1 = SortedSet(41, 12, 23, 43, 1, 72)
// 应用toBuffer()方法
val result = s1.toBuffer
// 显示输出
for (elem <- result)
println(elem)
}
}
1
12
23
41
43
72
阅读更多:Scala 教程
极客教程