1. Overview
In this tutorial, we’ll discuss different approaches to copying the elements of an array to another array in Scala.
2. Using Array as a Variable Argument
We know that the Array constructor accepts variable arguments. So, instead of passing all the array elements to another array, we can pass the entire array and use the splat operator in the constructor:
val array1 = Array(1, 2, 3, 4)
var array2 = Array(array1: _*)
The above statement creates a new copy of array2 in the memory. This means that both array1 & array2 will point to different arrays in the memory:
assert(array2 != array1)
Therefore, any change in array2 won’t affect array1 and vice versa:
array2(1) = 5
assert(array1(1) != array2(1)) // array2(1) = 2
3. Using the Array.copyToArray() Method
The copyToArray() method of class Array provides a better way to copy the elements of an array to another. It simply accepts an array that we want to fill:
val array1 = Array(1, 2, 3, 4)
var array2 = new Array[Int](4)
array1.copyToArray(array2)
Here, we’ve called the copyToArray() method on array1 and passed the newly created array2 as the parameter. Moreover, we should note that the size of array2 is 4. Thus, we can copy all the elements of array1 into array2.
Consequently, it stops copying based on two conditions – either all the elements of array1 have been copied or the end of the array2 is reached
Also, one of the overloaded methods accepts three parameters – an array that is to be filled, the start index in the array, and the length which signifies the number of elements to be copied:
def copyToArray[B >: A](xs: Array[B], start: Int, len: Int): Int
It also stops copying based on three conditions:
- all of the elements of array1 have been copied
- or the end of array2 is reached
- or the len number of elements have been copied
Let’s see an example:
array1.copyToArray(array2, 0, 4)
assert(array2(0) == 1)
assert(array2(3) == 4)
Here, we have called the copyToArray() method on array1 and passed the newly created array2 as the first parameter. Then, the second parameter, 0, is the start index of array2. And finally, the third parameter, 4, signifies the number of elements we need to copy from array1 to array2.
Moreover, both the arrays have separate memory locations allotted to them:
assert(array2 != array1)
4. Using the Array.map() Method
We can also use the map() method of class Array to copy all the elements of array1 to array2.
For this, we can pass the identity function to the map() method that essentially returns whatever we pass to it.
Let’s see an example:
val array1 = Array(1, 2, 3, 4)
var array2 = array1.map(identity)
assert(array2(0) == 1)
assert(array2(3) == 4)
Certainly, both the arrays have separate memory locations allotted to them:
assert(array2 != array1)
5. Using Array.clone() Method
If we check the definition of class Array in Scala, we can see that it extends the java.lang.Cloneable interface:
final class Array[T] extends java.io.Serializable with java.lang.Cloneable
Thus, we can use the clone() method to get the exact duplicate of array1, which also holds a different memory location:
val array1 = Array(1, 2, 3, 4)
var array2 = array1.clone()
assert(array2 != array1)
assert(array2(0) == 1)
assert(array2(3) == 4)
However, we should avoid using the clone() method because of its obvious bad design decisions.
6. Can We Use the Assignment Operator?
We might think that the simplest way of copying an array into another is using an assignment operator:
val array1 = Array(1, 2, 3, 4)
var array2 = array1
However, this may not be the right way to do this. The reason is that the assignment operator doesn’t actually create a new array. Instead, it simply points array2 to array1.
So, any change in array2 will result in the change in array1 and vice versa:
assert(array1(1) == array2(1))
array2(1) = 5
assert(array1(1) == 5)
Therefore, assigning an array to another should not be considered a way of copying array elements to another.
7. Conclusion
In this article, we’ve seen various approaches for copying the elements of an array into another. Which approach fits best? Well, the first three approaches – using the given array as a vararg, using the Array.copyToArray() and Array.map() methods – that we mentioned above work well. The clone() method and the assignment operator may not be good options for copying an array to another.
As always, the complete code samples for this article can be found over on GitHub.