带有分隔符的 Scala Queue mkString() 方法和示例
mkString()方法用于在字符串中显示队列的所有元素以及分隔符。
Method Definition: def mkString(sep: String): String
Return Type: It returns all the elements of the queue in a string along with a separator.
示例 #1:
// Scala program of mkString()
// method
// Import Queue
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a queue
val q1 = Queue(1, 2, 3, 4)
// Print the queue
println(q1)
// Applying mkString method
val result = q1.mkString(", ")
// Display output
print("Queue: " + result)
}
}
输出:
Queue(1, 2, 3, 4)
Queue: 1, 2, 3, 4
示例 #2:
// Scala program of mkString()
// method
// Import Queue
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a queue
val q1 = Queue("Geeks", "Will", "Rule")
// Print the queue
println(q1)
// Applying mkString method
val result = q1.mkString(", ")
// Display output
print("Queue: " + result)
}
}
输出:
Queue(Geeks, Will, Rule)
Queue: Geeks, Will, Rule