1

Can I copy from one Array[T] to another and specify the source start index?

val a = (1 to 10).toArray
val b = new Array[Int](8)
// specify target array, destination start index, and length.
// can i specify source start index?
a.copyToArray(b, 3, 2)

// b = Array[Int] = Array(0, 0, 0, 1, 2, 0, 0, 0)

So, I can use Array.drop to get the desired behavior, however that seems inefficient?

0

2 Answers 2

1

How about using slice?

val sourceStartIndex = 1
val destinationStartIndex = 3
val length = 2
a.slice(sourceStartIndex, sourceStartIndex + length)
 .copyToArray(b, destinationStartIndex, length)

It'll be more efficient than drop if length is small.

Sign up to request clarification or add additional context in comments.

Comments

0

I think, you are looking for Array.copy(a, srcStart, b, 3, 2)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.