Scala SortedSet toString() 方法示例
toString() 方法用于返回由 SortedSet 的所有元素组成的字符串。
方法定义:def toString(): String
返回类型:返回由 SortedSet 的所有元素组成的字符串。
示例1:
// Scala toString() 方法示例
// 导入 SortedSet 类
import scala.collection.immutable.SortedSet
// 创建对象
object GfG
{
// 主函数
def main(args:Array[String])
{
// 创建 SortedSet
val s1 = SortedSet(1, 2, 3, 4, 5)
// 应用 toString 方法
val result = s1.toString
// 显示输出
println(result)
}
}
TreeSet(1, 2, 3, 4, 5)
示例2:
// Scala toString() 方法示例
// 导入 SortedSet 类
import scala.collection.immutable.SortedSet
// 创建对象
object GfG
{
// 主函数
def main(args:Array[String])
{
// 创建 SortedSet
val s1 = SortedSet(41, 12, 23, 43, 1, 72)
// 应用 toString 方法
val result = s1.toString
// 显示输出
println(result)
}
}
TreeSet(1, 12, 23, 41, 43, 72)
阅读更多:Scala 教程