It is always better to work with immutable types. So I'll change the return type into List[Int]
. You can just do:
def printNTimes(items: List[Int], n: Int): List[Int] = {
items.flatMap(i => Vector.fill(n)(i))
}
Then running:
println(printNTimes(List(1,2,4), 3))
will output:
List(1, 1, 1, 2, 2, 2, 4, 4, 4)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…