带有开头、分隔符和结尾的 Scala Iterator addString() 方法示例
addString 方法用于将 Scala 迭代器的元素附加到 String Builder。此方法与addString()方法相同,但这里还包括开始、分隔符和结束。
Method Definition: def addString(b: mutable.StringBuilder, start: String, sep: String, end: String): mutable.StringBuilder
Where, sep is the separator stated.
Return Type: It returns the elements of the Iterator in the String Builder and a start, a separator and an end is also included here.
示例 #1:
Scala
// Scala program of addString() method
// with a start, a separator and an
// end
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a Iterator
val m1 = Iterator(1, 3, 4, 8)
// Applying addString method
val result = m1.addString(new StringBuilder(), "=>", "|", ".")
// Displays output
println(result)
}
}
Scala
// Scala program of addString() method
// with a start, a separator and an
// end
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a Iterator
val m1 = Iterator(1, 3, 3, 8)
// Applying addString method
val result = m1.addString(new StringBuilder(), "=>", "|", ".")
// Displays output
println(result)
}
}
输出:
=>1|3|4|8.
示例 #2:
斯卡拉
// Scala program of addString() method
// with a start, a separator and an
// end
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a Iterator
val m1 = Iterator(1, 3, 3, 8)
// Applying addString method
val result = m1.addString(new StringBuilder(), "=>", "|", ".")
// Displays output
println(result)
}
}
输出:
=>1|3|3|8.
因此,这里的相同元素不会像地图中那样被删除。