2015-05-29 105 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); 

這適用於標準數組列表。