Oct
13
|
The following utility method will allow you to merge any number of arrays.
public static <T> T[] arrayMerge(T[]... arrays) { // Determine required size of new array int count = 0; for (T[] array : arrays) { count += array.length; } // create new array of required class T[] mergedArray = (T[]) Array.newInstance( arrays[0][0].getClass(),count); // Merge each array into new array int start = 0; for (T[] array : arrays) { System.arraycopy(array, 0, mergedArray, start, array.length); start += array.length; } return (T[]) mergedArray; }
Array ( ) 11 Responses to “How can I merge two Java arrays into one combined array?”
Leave a Reply
You must be logged in to post a comment.
May 5th, 2009 at 4:12 pm
Here is another variant…
public static T[] merge(T[]… arrays) {
List list = new ArrayList();
for( T[] array : arrays )
list.addAll( Arrays.asList( array ) );
return (T[]) Array.newInstance( arrays[0][0].getClass(), list.size() );
}
May 18th, 2010 at 11:20 am
public static void merge(String[] a, String[] b)
{
List lst = new ArrayList(Arrays.asList(a));
lst.addAll(Arrays.asList(b));
Object[] result = lst.toArray();
System.out.println(“Merged output result is “+Arrays.toString(result));
}
December 28th, 2010 at 1:31 am
hi
April 8th, 2011 at 4:01 am
yo
August 8th, 2011 at 6:28 pm
public class array1 {
public static void merge(String[] a, String[] b)
{
List lst = new ArrayList(Arrays.asList(a));
lst.addAll(Arrays.asList(b));
Object[] result = lst.toArray();
Arrays.sort(result);
System.out.println(“Merged output result is”+Arrays.toString(result));
}
public static void main(String arg[]){
String a[]={“1″,”2″,”3″,”4″,”5”};
String b[]={“2″,”3″,”1″,”4″,”5”};
array1.merge(a, b);
}
}
November 10th, 2011 at 10:48 pm
All the variants posted here in the comments are pointless, unefficient and, most of all, get rid of the extremely efficient System.arraycopy.
November 15th, 2011 at 10:56 pm
@Emanuele why dont you tell us your genius-blessed own methods?
December 12th, 2011 at 11:41 pm
@Fmanuele: my favourite version is the one posted into the article. I was going to design my own implementation using System.arraycopy, in a similar fashion to this article, when I came across to it. I just simply adapted to my purposes, since I found it very clever.
March 28th, 2012 at 11:49 pm
This versions require Java 6, as they use Arrays.copyOf()
public static T[] concat(T[] first, T[]… rest) {
int totalLength = first.length;
for (T[] array : rest) {
totalLength += array.length;
}
T[] result = Arrays.copyOf(first, totalLength);
int offset = first.length;
for (T[] array : rest) {
System.arraycopy(array, 0, result, offset, array.length);
offset += array.length;
}
return result;
}
May 29th, 2012 at 6:04 pm
Thanks! =)
August 3rd, 2012 at 1:25 pm
for o(n) complexity 3 array merge
public static ArrayList merge(int al[], int bl[], int cl[]) {
ArrayList tl = new ArrayList();
int ai, bi, ci;
ai = bi = ci = 0;
while (true) {
if (ai != al.length) {
if (bi != bl.length) {
if (al[ai] < bl[bi]) {
if (ci != cl.length) {
if (al[ai] < cl[ci]) {
tl.add(al[ai]);
ai++;
} else {
tl.add(cl[ci]);
ci++;
}
} else {
tl.add(al[ai]);
ai++;
}
} else {
if (ci != cl.length) {
if (bl[bi] < cl[ci]) {
tl.add(bl[bi]);
bi++;
} else {
tl.add(cl[ci]);
ci++;
}
} else {
tl.add(bl[bi]);
bi++;
}
}
} else {
if (ci != cl.length) {
if (al[ai] < cl[ci]) {
tl.add(al[ai]);
ai++;
} else {
tl.add(cl[ci]);
ci++;
}
} else {
tl.add(al[ai]);
ai++;
}
}
} else if (bi != bl.length) {
if (ci != cl.length) {
if (bl[bi] < cl[ci]) {
tl.add(bl[bi]);
bi++;
} else {
tl.add(cl[ci]);
ci++;
}
} else {
tl.add(bl[bi]);
bi++;
}
} else if (ci != cl.length) {
tl.add(cl[ci]);
ci++;
} else {
break;
}
}
return tl;
}