interleave

Category: Arrays
Author: Benson Limketkai
Book Chapter: 5.4
Problem: interleave
Write a static method interleave that accepts two arrays of integers as parameters. The method returns a new array whose elements are the same as the elements of the two array parameters woven together.

If one array is shorter than the other, then after all the elements of the shorter array are woven in, the remaining elements of the longer array are just copied in sequence to the end of the returned array. The following example should make this all clear.

Assuming the following arrays have been declared:

     int[] a1 = { 1, 2, 3, 4, 5 };
     int[] a2 = { 6, 7, 8, 9, 10 };
     int[] a3 = { 11, 12, 13 };

Here are some example calls to the method and their expected return results.

    +--------------------+-----------------------------------+
    | Call               | Value Returned                    |
    +--------------------+-----------------------------------+
    | interleave(a1, a2) | { 1, 6, 2, 7, 3, 8, 4, 9, 5, 10 } |
    +--------------------+-----------------------------------+
    | interleave(a1, a3) | { 1, 11, 2, 12, 3, 13, 4, 5 }     |
    +--------------------+-----------------------------------+
    | interleave(a2, a3) | { 6, 11, 7, 12, 8, 13, 9, 10 }    |
    +--------------------+-----------------------------------+
    | interleave(a1, a1) | { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 }  |
    +--------------------+-----------------------------------+

For this problem, you may assume that the first array will always have a length greater than or equal to that of the second array, i.e. the first array will never be shorter than the second array.