Search code examples
javaarraysfill

How to fill an array with elements from another array of different size? (leaving non existent indexes as zero)


SCENARIO 1: if main array length is < 8

Declaration:

int[] mainArray = new int[] { 1, 2, 3, 4, 5 } // no minimum number of elements
int[] arrayOne = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 }; // must have 8 elements

I want to add the values from mainArray into arrayOne, with the spare elements being left as zero.

Desired array:

int[] arrayOne = new int[] { 1, 2, 3, 4, 5, 0, 0, 0 }; // must have 8 elements

SCENARIO 2: if main array length is > 8

Declaration:

int[] mainArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } // no minimum number of elements
int[] arrayOne = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 }; // must have 8 elements
int[] arrayTwo = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 }; // must have 8 elements

I want to add the the first 8 values from mainArray into arrayOne, and then the remaining values in arrayTwo, leaving the other indexes as zero (you'll see the 9 and 10 on the right of the second array, so arrayOne is left to right, arrayTwo is right to left. If there was an arrayThree, that would be left to right again)

Desired arrays:

int[] arrayOne = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }; // must have 8 elements
int[] arrayTwo = new int[] { 0, 0, 0, 0, 0, 0, 10, 9 }; // must have 8 elements

Solution

  • Try this.

    static int copy(int[] mainArray, int start, int[] array) {
        int mainArrayLength = mainArray.length;
        if (start >= mainArrayLength) return start;
        int length = Math.min(mainArrayLength - start, array.length);
        System.arraycopy(mainArray, start, array, 0, length);
        return start + length;
    }
    
    static void reverse(int[] array) {
        for (int i = 0, j = array.length - 1; i < j; ++i, --j) {
            int temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }
    }
    
    static void copy(int[] mainArray, int[] arrayOne, int[] arrayTwo) {
        int start = copy(mainArray, 0, arrayOne);
        copy(mainArray, start, arrayTwo);
        reverse(arrayTwo);
    }
    

    and

        int[] mainArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int[] arrayOne = {0, 0, 0, 0, 0, 0, 0, 0};
        int[] arrayTwo = {0, 0, 0, 0, 0, 0, 0, 0};
        copy(mainArray, arrayOne, arrayTwo);
        System.out.println("arrayOne = " + Arrays.toString(arrayOne));
        System.out.println("arrayTwo = " + Arrays.toString(arrayTwo));
    

    output

    arrayOne = [1, 2, 3, 4, 5, 6, 7, 8]
    arrayTwo = [0, 0, 0, 0, 0, 0, 10, 9]
    

    Do this if you want to copy to more than two arrays without reversing them.

    static void copy(int[] mainArray, int[]... arrays) {
        int start = 0;
        for (int[] array : arrays)
            start = copy(mainArray, start, array);
    }
    

    and

    int[] mainArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
    int[] arrayOne = {0, 0, 0, 0, 0};
    int[] arrayTwo = {0, 0, 0, 0, 0};
    int[] arrayThree = {0, 0, 0, 0, 0};
    copy(mainArray, arrayOne, arrayTwo, arrayThree);
    System.out.println("arrayOne = " + Arrays.toString(arrayOne));
    System.out.println("arrayTwo = " + Arrays.toString(arrayTwo));
    System.out.println("arrayThree = " + Arrays.toString(arrayThree));
    

    output

    arrayOne = [1, 2, 3, 4, 5]
    arrayTwo = [6, 7, 8, 9, 10]
    arrayThree = [11, 12, 13, 0, 0]