I want to assign a part of an array to a part of another array in Scala. For example, I want to do the Scala or Java equivalent of the following Python code.
x[i:j] = y[k:l]
How can I do that in Scala or even Java?
I want to assign a part of an array to a part of another array in Scala. For example, I want to do the Scala or Java equivalent of the following Python code.
x[i:j] = y[k:l]
How can I do that in Scala or even Java?
You can use a combination of .patch and .slice:
scala> val a = Array.range(1, 20)
a: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19)
scala> val b = Array.range(30, 50)
b: Array[Int] = Array(30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49)
scala> a.patch(5, b.slice(5, 10), 5)
res5: Array[Int] = Array(1, 2, 3, 4, 5, 35, 36, 37, 38, 39, 11, 12, 13, 14, 15, 16, 17, 18, 19)
The parameters of .slice are:
i in your python exemple)the array to insert (y[k:l], here using .slice to select from k to l)
the number of element to replace in the array (unclear about what happens in your exemple when i:j is smaller than k:l, but I would guess it would be j-i here)