dono
dono

Reputation: 149

Return Arraylist of ints

I'm new to Java (1month). I've been trying to do the below unsuccessfully, any help would be appreciated. What I'm trying to have is:

  1. Main passes int array to method perm2.
    perm2(new int[]{1,2,3});

  2. perm2 will make permutations of it and add to an ArrayList and return it.

This is what I have so far. As you can see it just prints out the permutations.
Problem is I can't add them to an ArrayList and return.

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

Output:

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

I don't really want to output them with sysout. I just want them returned in ArrayList of int arrays.

Upvotes: 0

Views: 1054

Answers (4)

Sphinx
Sphinx

Reputation: 21

You have to change the method perm2 to return the array and the signature of the methods.

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

Upvotes: 0

Boann
Boann

Reputation: 50041

Use a return type of List<int[]> (or you prefer, ArrayList<int[]>) from the main perm method:

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

Then in the second method, pass the list to it as another parameter so it can add each permutation. Note that it has to clone the array, otherwise it would be adding the same (changing) array object over and over, which would end up with a list of several copies of the final permutation only.

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

To call it:

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

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

Something that's not necessary, but you might consider, is changing the parameter of the first perm2 method from int[] s to int... s. Then you could invoke it as simply perm2(1, 2, 3) instead of perm2(new int[] { 1, 2, 3 }), although it would still accept the explicit array too.

Upvotes: 1

Januson
Januson

Reputation: 4841

ArrayList can only hold objects. You can use wrapper class Integer for your int (primitive) values.

Upvotes: 1

Ashalynd
Ashalynd

Reputation: 12573

Your first perm2 method should at least return the new object. The original object isn't being modified in that code.

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

Upvotes: 1

Related Questions