How do I merge/concat two arrays to one? I see this post, however I need a solution that can concat two different arrays that both extend the same class. eg foo1 extends Object and foo2 extends Object, then I need to concat foo1[] and foo2[] to an Object[] array.
-
3dup http://stackoverflow.com/questions/80476/how-to-concatenate-two-arrays-in-java – Amir Afghani May 24 '13 at 15:21
-
1How does http://stackoverflow.com/questions/80476/how-to-concatenate-two-arrays-in-java not cover you? Duplicate. – Menelaos May 24 '13 at 15:21
-
What part of the solutions proposed in each of the answers to that question could not be implemented in your case? – Paul Vargas May 24 '13 at 15:24
-
As figuring out the right type seems to be problem it might be a good idea to share a little more thoughts about this with us. The chance that foo1 and foo2 both extend Object is pretty high in java. And if in all of these cases Object[] is the right choice the question is in fact kind of superfluous.. – Bernd Ebertz May 24 '13 at 15:37
5 Answers
Well, first of all, java arrays
have fixed memory and size, so to concat two arrays you have to create a new one with the size of arrOne.length + arrTwo.length
and just iterate over the values of arrays and add them to the new array.
Object[] arrThree = new Object[arrTwo.length + arrOne.length];
int index = arrTwo.length;
for (int i = 0; i < arrTwo.length; i++) {
arrThree[i] = arrTwo[i];
}
for (int i = 0; i < arrOne.length; i++) {
arrThree[i + index] = arrOne[i];
}
This would work without any additional utils - plain java. arrOne
and arrTwo
are considered as already existing ones.
-
2`System.arraycopy(arrOne, 0, arrThree, 0, arrOne.length); System.arraycopy(arrTwo, 0, arrThree, arrOne.length, arrTwo.length);` – Joop Eggen May 24 '13 at 15:26
-
Oh, thanks. Didn't know about that. Example is here -http://www.tutorialspoint.com/java/lang/system_arraycopy.htm - in case the TS would like to see it. But still, it could be not clear for the TS. – user May 24 '13 at 15:27
you can use
ArrayUtils.addAll method. accord to doc
Adds all the elements of the given arrays into a new array.
The new array contains all of the element of array1 followed by all of the elements array2. When an array is returned, it is always a new array.
so you can use like
resultArray[] = ArrayUtils.addAll(firstArray, SecondArray);

- 54,068
- 14
- 92
- 112
create your Object[] array,then do a first loop on your foo1[] to add manually to the object[],then repeat for foo2[].
barbarian,yet simple and efficient

- 245
- 3
- 15
One liner solution:
Object[] both = ArrayUtils.addAll(array1,array2);

- 6,926
- 4
- 43
- 56
-
2
-
1Oh right -- http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/ArrayUtils.html . That works only if you have commons.lang – dchhetri May 24 '13 at 15:24
Why don't you just create an Array of type object that is the combined length of the foo1[] + the length of the foo2[]. Then either iterate each array and add to the Object[] or use the answer in the link you provided: How can I concatenate two arrays in Java?
Or better still work with ArrayList instead and use the .addAll() method