Java,如何添加2个不同大小的arrayLists

Java, how to add 2 arrayLists of different sizes

所以我目前正在为我正在做的课程开发一个项目,我正在编写一个方法,需要将两个包含整数的 ArrayList 相加,并将总和设置在一个新的 ArrayList 中。目前我有这个,工作正常

    public BigInt add(BigInt otherBigInt) {
    BigInt result = new BigInt();
            int length = digitList.size() - 1;
            int lengthOther = otherBigInt.digitList.size() - 1;
            int temp = 0;
            int whole = 0;
            int carry = 0;



    for (int i = length; i >= 0; i--){
        temp = (digitList.get(i) + otherBigInt.digitList.get(i));
        temp += carry;
        // temp is equal to the sum of this(i) and otherBigInt(i), plus any number carried over.
        if (temp >= 10){
            whole = temp - 10;
            carry = 1;

            result.digitList.add(whole); 
        }
        else if (temp < 10){
            carry = 0;
            result.digitList.add(temp);
        }
    }

    if (carry == 1){
        result.digitList.add(carry);
    }
    //adds any remaining carried number after for loop
    // Supply this code.

    return result;
}

但是,目前该方法仅在 arrayLists 大小相同时才有效,我将如何修改该方法以适应不同大小的列表?这方面的一个例子是包含 735934 和 68945 的两个列表给出结果 804879.

p.s。 不确定是否需要它,(在这里发布仍然是新手)但我目前添加的两个列表是 7359 和 6894,给出答案 14253.

令两位数组为a1[0 ... n1]a2[0 ... n2]。现在你的算法应该是这样的:

add(a1, a2):
   min_length = min{a1.length, a2.length}
   result[0 ... max{a1.length, a2.length} + 1]
   carry = 0
   for(i in [0 ... min_length - 1])
       result[i] = carry + a1[i] + a2[i] 
       carry = result[i] / 10 
       result[i] %= 10
   while(i < a1.length)
       result[i] = a1[i] 
   while(i < a2.length)
       result[i] = a2[i] 
   result[i] = carry

请注意,您必须添加剩余的数字,以防它们的大小不同。我假设数字是按顺序存储的,即 a[0] 是第一个数字。

如果我的假设是正确的,那么您正在尝试模拟使用两个列表将两个数字相加的情况,其中数字的一位占用列表的一个索引。

最简单的解决方案是假设最短的列表有零而不是没有值,然后添加到两个列表的最大值:

public BigInt add(BigInt otherBigInt) {
    BigInt result = new BigInt();
    int length = Math.max(digitList.size(), otherBigInt.digitList.size()) - 1;
    int lengthOther = otherBigInt.digitList.size() - 1;
    int temp = 0;
    int whole = 0;
    int carry = 0;

    for (int i = length; i >= 0; i--){
        temp = ( checkAndGet(digitList, i) + checkAndGet(otherBigInt.digitList, i) );
        temp += carry;
        // temp is equal to the sum of this(i) and otherBigInt(i), plus any number carried over.
        if (temp >= 10) {
            whole = temp - 10;
            carry = 1;

            result.digitList.add(whole); 
        }
        else {
            carry = 0;
            result.digitList.add(temp);
        }
    }

    if (carry == 1){
        result.digitList.add(carry);
    }

    //adds any remaining carried number after for loop
    // Supply this code.

    return result;
}

// if the index position being retrieved is larger than the size, assume 0    
private int checkAndGet(List<Integer> input, position) {
    return (input.size() < position) ? input.get(position) : 0;
}

我会向后遍历较小的数组,同时将索引添加到一起:

ArrayList<Integer> toIterate = (array1.size() > array2.size)? a1 : a2;
ArrayList<Integer> seperate = (array1.size() > array2.size)? a2 : a1;

for (int i = toIterate.size - 1; i >= 0; i --) {
if (seperate.get(i) != null) {
arrayResult.add(toIterate.get(i) + seperate.get(i));
}
else {

arrayResult.add(toIterate.get(i));

}
}