如何在2个arraylists中添加整数? JAVA

时间:2015-05-29 13:33:53

标签: java arraylist

假设我有arraylist A = {9,5,3,7}和arraylist B = {4,9,8,7,5}。 (两个arraylists的长度可以相同或不同。) 这些arraylists代表数字(但写得最不重要的第一顺序)

我想创建一个arraylist(结果),其中包含每个数字的总和以及其他arraylist中的对应部分。如果总和> 9然后剩下的数字继续到下一个数字。以上内容与:7359 + 57894 = 65253

相同

结果必须是arraylist:result = {3,5,2,5,6}。

这就是我的尝试:

    BigInt result = new BigInt();

    int temp=0;
    int carry=0;

    BigInt bigger = this;
    BigInt smaller = otherBigInt;

    if(this.lessOrEqual(otherBigInt)){
        smaller = this;
        bigger = otherBigInt;
    }

    for(int i=0; i<bigger.digitList.size(); i++){
      temp= bigger.digitList.get(i)+smaller.digitList.get(i)+carry;         
      carry = temp/10;
      result.digitList.add(i,temp%=10);

    }

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

我不知道我的代码有什么问题..请帮忙

3 个答案:

答案 0 :(得分:1)

PFB对您问题的确切答案:

int size, carry = 0, temp = 0;

size = Math.max(al1.size(), al2.size());

ArrayList<Integer> al = new ArrayList<Integer>(size);

for (int i = 0; i < size; i++) {

    if (al1.size() > i && al2.size() > i)
        temp = carry + al1.get(i) + al2.get(i);
    else if (al1.size() > i)
        temp = carry + al1.get(i);
    else
        temp = carry + al2.get(i);

    carry = temp / 10;
    al.add(temp % 10);
}

System.out.println(al);

答案 1 :(得分:0)

我将这个方法添加到我认为你拥有的课程中。 如果你这样做会很有帮助,所以人们可以一次性运行你的代码。

对于那些想知道的人:他并不是要将一个数组的元素添加到另一个数组中,但他的意思是将两个数组视为数字(拆分成数字)并将这些数字的总和创建为输出数组

我在评论中描述了一个ArrayIndexOutOfBoundsException 你检查我是否&lt;更大的索引,但忘记测试我是否&lt; smallerIndex。当你的i太高(高于tinyIndex中的元素)时,你会得到错误。

下面我更改了你的代码以测试是否还有更小的部分可以加起来,或者你是否已经用尽了。

还有其他方法可以做到这一点,例如你可以在smallInts的末尾添加0来使它们具有相同的长度,或者在bigInt上创建一个方法(getNthDigit(int i)返回数字,如果没有找到则为0) )

public class BigInt
{
    private ArrayList<Integer> digitList = new ArrayList<Integer>();

    public BigInt(Integer... ints) {
        this.digitList.addAll(Arrays.asList(ints));
    }

    public BigInt add(BigInt otherBigInt) {
        BigInt result = new BigInt();

        int carry = 0;

        BigInt bigger = this;
        BigInt smaller = otherBigInt;

        if (this.lessOrEqual(otherBigInt)) {
            smaller = this;
            bigger = otherBigInt;
        }

        for (int i = 0; i < bigger.digitList.size(); i++) {
            int temp;
            if (i < smaller.digitList.size()) {
                temp = bigger.digitList.get(i) + smaller.digitList.get(i) + carry;
            } else {
                temp = bigger.digitList.get(i) + carry;
            }
            carry = temp / 10;
            result.digitList.add(i, temp % 10);
        }

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

    private boolean lessOrEqual(BigInt other) {
        return other.digitList.size() > digitList.size();
    }

    public String toString() {
        return Arrays.toString(digitList.toArray());
    }

    public static void main(String[] argv) {
        BigInt first = new BigInt(9,5,3,7);
        BigInt second = new BigInt(4,9,8,7,5);

        BigInt result = first.add(second);
        System.out.println(result);
    }
}

您可以在BigInt上使用类似OO的方法来避免此问题,同时避免添加if / else:

public int getNthDigit(int digit) {
    if (digit < digitList.size()) {
         return digitList.get(digit);
    } else {
        return 0;
    }
}

输出:

[3,5,2,5,6]

答案 2 :(得分:0)

这是我解决问题的版本:

    ArrayList<Integer> bigger = (A.size() >= B.size()) ? A : B;
    ArrayList<Integer> smaller = (A.size() < B.size()) ? A : B;
    ArrayList<Integer> C = new ArrayList();

    int idx = 0, sum, carr = 0;

    for (Integer i : bigger) {
        sum = (i + carr + ((idx < smaller.size()) ? smaller.get(idx++) : 0));
        carr = sum / 10;
        C.add(sum % 10);
    }

    if (carr == 1 ) C.add(carr);
    System.out.println("Array: " + C );

这适用于标准的ArrayList。