Q: Adding two arrays of ints I'm having trouble figuring out how to add two integer arrays in Java (such that they act like two separate numbers). So if one of my arrays is [1, 1, 2] and the other [2, 9, 8] the program would return [4, 1, 0]. Or if my first array is [9, 8] and my second is [2, 2], it would return [1, 2, 0].
I have running code, the primary issue being that it can't add arrays of differing sizes (the index j goes into the negatives, generating a index out of bounds exception). I have no idea how to go about fixing this, without messing the code up.
int j = h.size - 1;
int carry = 0;
int k = temp3.length - 1;


for (int i = this.size - 1;  i >= 0; i--){
    int sum = 0;
    sum = temp1[i] + temp2[j] + carry;
    j--;

    if (sum > 9){
        carry = 1;
    }
    else
        carry = 0;
    int lastDigit = sum%10;
    temp3[k] = lastDigit;
    k--;
}
temp3[k] = carry;


A: If you wanted to do this through arrays (or any other means really) You would need to pad the front of one of the numbers with 0's so that they are the same length. So with arrays you would need to create a third array with all 0s and then change starting from the end with the numbers of the smaller array and use that to compare with the larger array.

A: Convert the arrays to integers (or BigIntegers):
static BigInteger asBigInt(int[] arr) {
    BigInteger n = BigInteger.ZERO;
    for (int i : arr)
        n = n.multiply(BigInteger.TEN).add(BigInteger.valueOf(i));
    return n;
}

And use addition:
System.out.println(asBigInt(arr1).add(asBigInt(arr2))); // 410

