Write a method weave that takes two arrays of ints, a and b, and that returns an array that contains the elements of a and b in the order.a[0], b[0], a[1], b[1], etc. If one of the arrays a or b is longer than the other, just add the extra elements at the end of the array.
public class Main {
public static int[] merge(int[] a, int[] b) {
int[] c = new int[a.length + b.length];
for (int i = 0, j = 0, k = 0; i < a.length || j < b.length; i++, j++) {
if (i < a.length) {
c[k++] = a[i];
}
if (j < b.length) {
c[k++] = b[j];
}
}
return c;
}
}
Comments
Leave a comment