How to concatenate arrays in Java?
Array concatenation is one of the hot topics of Java. There are many instances when a user
needs to join two or more arrays together. In this article, we are going to look at various
methods for array concatenation.
For starters, let us look at the easiest method to join two or more arrays. We do this
by using only for loops. Let us look at the example code.
Output
Let us look at the output of the above example code.
See! The two arrays are concatenated. In this method, we just take the two arrays, and the combined length of both the arrays gives the length of resultant array. Initially the first array is inserted into the resultant array and a counter is incremented every time the loop runs. After the first loop is terminated, the second loop comes into picture. Now, the resultant array`s index will begin from the current counter position, in the second loop. The contents of second array will be inserted into the resultant array and the counter will be incremented each time the loop runs. Finally, the resultant array is displayed.
Using arraycopy()
Now let us look at another method for array concatenation, which is using arraycopy() method. This requires less line of code when compared to the previous method. Let us look at the example code for this method.
Output
Let us look at the output of the above example code.
When you compare this program with the previous one, the difference occurs at
System.arraycopy(). Let us talk about the arraycopy() method.
The arraycopy method copies a source array from a specific beginning position
to the
destination array from the mentioned position.
It contains five parameters. The first parameter tells about the source array.
The
second parameter indicates the position from which the array is to be copied, also known as
the source position. The third parameter gives the destination array. The
fourth parameter
gives the destination position, which tells about the position where the resultant
array
should begin and finally the fifth parameter tells about the position where the resultant
array will end.
Let us refer the above example to understand the arraycopy() method a little
bit better.
This above function tells the program to copy array1 starting from index 0 to concatenated_array from index 0 to array1.length.
Similarly, the above function tells the program to copy array2 starting from index
0 to
concatenated_array from index array1.length to array2.length.
Finally, the resultant concatenated_array is displayed using the
toString() method. Try
printing out the concatenated_array directly and you will observe a weird output
being
displayed, which is the hashcode value of the object.
Summary
There are many other methods which can be followed to achieve array concatenation. As a
programmer, you should always look for alternative approach, and only then you can compare
which one is better.
From the above two methods, the first one involving only for loops and
the other one using arraycopy() method, I personally like to follow the second approach. In
the second method the lines of code written are less and more importantly the method
arraycopy() is build for that purpose.