2015-05-29 102 views
-1

可以说我有阵列列表A = {9,5,3,7}和阵列列表B = {4,9,8,7,5}。 (这两个的ArrayLists的长度可以是相同或不同。) 这些的ArrayList表示数字(但写在至少显著一阶)如何在2个数组列表中添加整数? JAVA

我想创建一个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; 

我不知道什么是错我的代码..请帮助

+11

A = {9,5,3,7},B = {4,9,8,7,5} - > {3,5,2,5,6} < - 这里您的逻辑是什么? – nafas

+0

请提供您现在得到的输出结果和您期望的结果 – novy1234

+0

提供正确的输入和期望输出 – Rajesh

回答

0

我添加的方法到类我想你了。 如果你这样做会有帮助,所以人们可以一次运行你的代码。

对于那些想知道的人:他并不意味着将一个数组的元素添加到另一个数组中,但他意味着将两个数组看作数字(将其分割为数字)并将这些数字的总和输出数组

我得到我在注释中描述一个ArrayIndexOutOfBoundsException 你检查我< biggerIndex却忘了测试,如果我< smallerIndex。当你变得太高时(比lowerIndex中的元素更高),你会得到错误。

下面我改变了你的代码来测试是否有更小的剩余部分加起来,或者如果你用完了。

还有其他的方法可以做到这一点,例如,你可以将0添加到smallerInts的末尾以使它们具有相同的长度,或者在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]

+0

它仍然不起作用..它说比较失败..我不知道为什么这些比较问题,因为这两个arraylists的元素都是整数。 –

+0

添加了类定义和我用来测试的主要方法,我的代码运行并给出了预期的输出 – Joeblade

+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); 
+0

非常简约:)只有在循环中需要temp,tmp%= 10应该是tmp%10我认为。 (不需要分配)。而不是三元表达式,你可以使用Math.max。 – Joeblade

+0

谢谢Joeblade。 @ Jeffrey.S你有没有试过这个? – Rajesh

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); 

这适用于标准数组列表。