2013-06-23 49 views
3

我被困在非常基本的將整數數組序列推向數組列表的方式。 我正在嘗試改變這個問題的polygenelubricants的解決方案,而不是打印它們,我將它們推到一個數組列表中。將整數數組推送到ArrayList

我的代碼:

public class Test { 

    static ArrayList<String> combinations; 
    public static void main(String args[]) { 
     Integer[] a3 = { 1, 2, 3, 4, 5 }; 
     comb(a3, 2); 
    } 
    public static void comb(Integer[] items, int k) { 
     Arrays.sort(items); 
     combinations = new ArrayList<String>(); 
     ArrayList<String> c1 = new ArrayList<String>(); 
     c1 = kcomb(items, 0, k, new Integer[k]); 
     System.out.println("from comb"); 
     for (String x : c1) { 
      System.out.println(x); 
     } 
    } 
    public static ArrayList<String> kcomb(Integer[] items, int n, int k, 
      Integer[] arr) { 
     if (k == 0) { 
      combinations.add(Arrays.toString(arr)); 
     } else { 
      for (int i = n; i <= items.length - k; i++) { 
       arr[arr.length - k] = items[i]; 
       kcomb(items, i + 1, k - 1, arr); 
      } 
     } 
     return combinations; 
    } 
} 

輸出:

from comb 
[1, 2] 
[1, 3] 
[1, 4] 
[1, 5] 
[2, 3] 
[2, 4] 
[2, 5] 
[3, 4] 
[3, 5] 
[4, 5] 

但是當我從字符串改變ArrayList中的類型整數[]如下,我正在冗餘輸出。

改變的代碼

public class Test { 

    static ArrayList<Integer[]> combinations; 
    public static void main(String args[]) { 
     Integer[] a3 = { 1, 2, 3, 4, 5 }; 
     comb(a3, 2); 
    } 
    public static void comb(Integer[] items, int k) { 
     Arrays.sort(items); 
     combinations = new ArrayList<Integer[]>(); 
     ArrayList<Integer[]> c1 = new ArrayList<Integer[]>(); 
     c1 = kcomb(items, 0, k, new Integer[k]); 
     System.out.println("from comb"); 
     for (Integer[] x : c1) { 
      System.out.println(Arrays.toString(x)); 
     } 
    } 
    public static ArrayList<Integer[]> kcomb(Integer[] items, int n, int k, 
      Integer[] arr) { 
     if (k == 0) { 
      combinations.add(arr); 
     } else { 
      for (int i = n; i <= items.length - k; i++) { 
       arr[arr.length - k] = items[i]; 
       kcomb(items, i + 1, k - 1, arr); 
      } 
     } 
     return combinations; 
    } 
} 

輸出

from comb 
[4, 5] 
[4, 5] 
[4, 5] 
[4, 5] 
[4, 5] 
[4, 5] 
[4, 5] 
[4, 5] 
[4, 5] 
[4, 5] 

人可以幫我指出我在做什麼錯?

感謝, 薩拉特

+0

確定它的'ArrayList '不'ArrayList ' – codeMan

+0

@codeMan,因爲我們得到a3中的整數組合,它應該是ArrayList 。我試圖在arraylist中存儲的是多組組合。 –

回答

1

有您的錯誤:combinations.add(arr);您總是在同一個陣列上工作arr

請記住,數組是對象並有一個引用。您保存與ArrayList相同的數組,並在之後不斷更改數組值。每次您在同一陣列上工作時,您總是可以獲得所有其他組合的最後組合的值。

因此,您需要克隆arr,然後才能將其添加到ArrayList以獲取新的引用。之前的代碼工作,因爲每個字符串都有自己的引用,因爲字符串是不可變的。

+0

謝謝。更改combinations.add(arr); ((Integer [])arr.clone()); –

2
public class Test { 

    static ArrayList<Integer[]> combinations; 
    public static void main(String args[]) { 
     Integer[] a3 = { 1, 2, 3, 4, 5 }; 
     comb(a3, 2); 
    } 
    public static void comb(Integer[] items, int k) { 
     Arrays.sort(items); 
     combinations = new ArrayList<Integer[]>(); 
     ArrayList<Integer[]> c1 = new ArrayList<Integer[]>(); 
     c1 = kcomb(items, 0, k, new Integer[k]); 
     System.out.println("from comb"); 
     for (Integer[] x : c1) { 
      System.out.println(Arrays.toString(x)); 
     } 
    } 
    public static ArrayList<Integer[]> kcomb(Integer[] items, int n, int k, 
      Integer[] arr) { 
     if (k == 0) { 
      combinations.add(arr); 
     } else { 
      for (int i = n; i <= items.length - k; i++) { 
       Integer[] arr1 = new Integer[arr.length]; 
       System.arraycopy(arr, 0, arr1, 0, arr.length); 
       arr1[arr.length - k] = items[i]; 
       kcomb(items, i + 1, k - 1, arr1); 
      } 
     } 
     return combinations; 
    } 
} 
+0

上次對我錯誤的回答抱歉。檢查這一個。 – stinepike

1

問題是你只創建一個Integer []數組 - 它被重用於每次調用kcomb,所以在該進程結束時,同一個數組已被多次添加到列表中,但是數組的內容只是最後一個組合。另外,你不需要使用Integer []來達到這個目的--int []是非常令人滿意的,並且效率更高。