Array concatenation

From Rosetta Code
Revision as of 18:50, 9 September 2009 by rosettacode>Mwn3d (Created task with Java)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
Array concatenation
You are encouraged to solve this task according to the task description, using any language you may know.

Show how to concatenate two arrays in your language. If this is as simple as adding a list to a list, so be it.

Java

From [1]: <lang java5>public static Object[] objArrayConcat(Object[] o1, Object[] o2) {

 Object[] ret = new Object[o1.length + o2.length];

 System.arraycopy(o1, 0, ret, 0, o1.length);
 System.arraycopy(o2, 0, ret, o1.length, o2.length);

 return ret;

}</lang>

Or with Collections simply call addAll: <lang java5>Collection list1, list2, list1And2; //...list1 and list2 are instantiated... list1And2 = new ArrayList(list1); //or any other Collection you want list1And2.addAll(list2);