我有两个val
s:
// 0~250::250~500::500~750::750~1000
val step_x_ranges = stepsAsString.mkString("::")
// 1::22::7::16
val step_y = histogram_final.map{case (x , y) => y}.mkString("::")
我希望将其保存为Array
,如:
0~250::1
250~500::22
500~750::7
750::1000::16
但仍未尝试: 请帮帮我
这是我的错误代码:
var toArray : Array[String] = Array()
for (i <- 0 to 3)
{
val ArrayRow = step_x_ranges(i) + "::" + step_y(i)
toArray = toArray :+ ArrayRow
}
println(toArray(0))
println(toArray(1))
结果:
0::4
~::8
答案 0 :(得分:3)
使用zip
:
scala> val step_x_ranges = Array("0~250", "250~500", "500~750","750~1000")
step_x_ranges: Array[String] = Array(0~250, 250~500, 500~750, 750~1000)
scala> val step_y = Array(1, 22, 7, 16)
step_y: Array[Int] = Array(1, 22, 7, 16)
scala> val result = step_x_ranges.zip(step_y).map{case (x, y) => s"$x::$y"}
res1: Array[String] = Array(0~250::1, 250~500::22, 500~750::7, 750~1000::16)
scala> scala.tools.nsc.io.File("test.txt").writeAll(result.mkString("\n"))
并且test.txt:
0~250::1
250~500::22
500~750::7
750~1000::16