2013-10-15 72 views
0

我是Java新手(1個月)。我一直在嘗試做下面的失敗,任何幫助將不勝感激。我想要有是:返回Arraylist ints

  1. 主要通過int數組到方法PERM2
    perm2(new int[]{1,2,3});

  2. PERM2將它置換,並添加到一個ArrayList並返回它。

這是我到目前爲止。正如你所看到的,它只是打印出排列。
問題是我不能將它們添加到ArrayList並返回。

public static void perm2(int[] s) { 
    int N = s.length; 
    int[] a = new int[N]; 
    for (int i = 0; i < N; i++) a[i] = s[i]; 
    perm2(a, N); 
} 
private static void perm2(int[] a, int n) { 
    if (n == 1) { 
     System.out.println(Arrays.toString(a)); 
     return; 
    } 
    for (int i = 0; i < n; i++) { 
     swap(a, i, n-1); 
     perm2(a, n-1); 
     swap(a, i, n-1); 
    } 
} 
private static void swap(int[] a, int i, int j) { 
    int c; 
    c = a[i]; a[i] = a[j]; a[j] = c; 
} 

輸出:

[2, 3, 1] 
[3, 2, 1] 
[3, 1, 2] 
[1, 3, 2] 
[2, 1, 3] 
[1, 2, 3] 

我真的不希望他們輸出,sysout。我只是希望他們在int數組的ArrayList中返回。

回答

1

使用的List<int[]>返回類型(或你喜歡,ArrayList<int[]>)從主燙髮方法:

public static List<int[]> perm2(int[] s) { 
    List<int[]> permutations = new ArrayList<>(); 
    perm2(permutations, s.clone(), s.length); 
    return permutations; 
} 

然後在第二個方法,列表傳遞給它作爲其他參數,以便它可以添加每個排列。請注意,它必須複製數組,否則它會一遍又一遍地添加相同(更改)的數組對象,最終只會得到最終置換的多個副本的列表。

private static void perm2(List<int[]> permutations, int[] a, int n) { 
    if (n == 1) { 
     permutations.add(a.clone()); 
     return; 
    } 
    for (int i = 0; i < n; i++) { 
     swap(a, i, n-1); 
     perm2(permutations, a, n-1); 
     swap(a, i, n-1); 
    } 
} 

要叫它:

List<int[]> permutations = perm2(new int[] { 1, 2, 3 }); 

for (int[] permutation : permutations) { 
    System.out.println(Arrays.toString(permutation)); 
} 

東西是沒有必要的,但你可以考慮,在改變第一PERM2方法的參數從int[] sint... s。然後你可以簡單地調用它perm2(1, 2, 3)而不是perm2(new int[] { 1, 2, 3 }),儘管它仍然會接受顯式數組。

+0

@blalasaadri'列表'很好。 'int []'是一個對象,而不是一個原始類型。 – Boann

1

ArrayList只能保存對象。您可以使用包裝類Integer作爲int(primitive)值。

+0

看到類似的問題[爲什麼Java集合不能直接存儲基元類型?(http://stackoverflow.com/questions/2504959/why-can-java-collections-not-directly-store-primitives -types) –

+1

但我認爲int []是一個對象。 (容器對象)否? – dono

1

你的第一個perm2方法應該至少返回新的對象。該代碼中未修改原始對象。

public static int[] perm2(int[] s) { 
    int N = s.length; 
    int[] a = new int[N]; 
    for (int i = 0; i < N; i++) a[i] = s[i]; 
    perm2(a, N); 
    return a; 
} 
private static void perm2(int[] a, int n) { 
    if (n == 1) { 
     System.out.println(Arrays.toString(a)); 
     return; 
    } 
    for (int i = 0; i < n; i++) { 
     swap(a, i, n-1); 
     perm2(a, n-1); 
     swap(a, i, n-1); 
    } 
} 
private static void swap(int[] a, int i, int j) { 
    int c; 
    c = a[i]; a[i] = a[j]; a[j] = c; 
} 
0

您必須更改方法perm2以返回數組和方法的簽名。

public static ArrayList<Integer> perm2(int[] s) { 
    ArrayList<Integer> a = new ArrayList<Integer>(); 
    for (int i = 0; i < a.size(); i++) { 
     a.add(s[i]); 
    } 
    perm2(a, a.size()); 
    return a; 
} 
private static void perm2(ArrayList<Integer> a, int n) { 
    if (n == 1) { 
     return; 
    } 
    for (int i = 0; i < n; i++) { 
     swap(a, i, n - 1); 
     perm2(a, n - 1); 
     swap(a, i, n - 1); 
    } 
} 
private static void swap(ArrayList<Integer> a, int i, int j) { 
    int c; 
    c = a.get(i); 
    a.add(i, a.get(j)); 
    a.remove(i + 1); 
    a.set(j, c); 
}