Answer to Question #172325 in Java | JSP | JSF for ikram

Question #172325

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. 


1
Expert's answer
2021-03-16T19:29:10-0400
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;
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog