3

I want to get different array elements combinations (permutations) of an array to a List. I swap the first and the last element of the array through a for loop and the combination (permutation) is added to the List. Then the second and the element before the last is swapped and added to the List, so and so forth. Suppose the array is arr[1,2,3,4,5,6,7], the first element added to the List would be arr[7,2,3,4,5,6,1]. The second element would be arr[7,6,3,4,5,2,1]. But what I end up getting is something like arr[7,6,5,4,3,2,1] for all the elements in the List.

The problem is that the elements added to the List are also modified correspondingly with the current modification of the array. I end up getting similar array elements in the List. What I want is different permutations or arrays with different combinations of elements. Can you please help me with this?

private List<Gate[]> generateSolutions(Gate[] solution) {
    List<Gate[]> sList= new ArrayList<>();
    int i, j;
    for (i = 0, j = solution.length - 1; i < solution.length / 2; i++, j--) {
        Gate temp;
        temp = solution[i];
        solution[i] = solution[j];
        solution[j] = temp;
        sList.add(solution);
    }
    return sList;
}
VRB
  • 31
  • 4
  • 6
    The `add` method of ArrayList does not create a copy of the elements you are adding to it. You are constantly adding the same Array to the list again and again. You'll probably need to change your code so that you copy the Array, modify the copy and then add that copy to your list. – OH GOD SPIDERS Oct 27 '20 at 16:51
  • 4
    Every time you call `sList.add(solution)`, you should instead be e.g. writing `sList.add(Arrays.copyOf(solution, solution.length))`. – Louis Wasserman Oct 27 '20 at 17:05
  • That really helped. Thank you for saving my time. :) – VRB Oct 27 '20 at 17:14

1 Answers1

-2
import java.util.ArrayList;
import java.util.List;

/**
 * Dev Parzival
 * Date : 27/10/2020 Time : 22:40
 * I have a confidence about my life that comes from standing tall on my own two feet.
 */
public class Permutation {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>(10);

        //Make sure that the list has unique numbers
        for(int i=0;i<3;i++)
            list.add(i+1);

        int n =list.size();
        Permutation permutation = new Permutation();

        //Generater permutation
        permutation.permute(list, 0, n-1);
    }

    private void permute(ArrayList<Integer> list, int l, int r) {
        //When l is equal to one less than the length of the list we will display it.
        if (l == r)
            System.out.println(list);
        else{
            //The l valuse specifies which position we want to fix
            for (int i = l; i <= r; i++) {
                //After diciding the position we fix it by swapping it.
                swap(list,l,i);
                //Here we are fixing l+1 th number
                permute(list, l+1, r);
                //Swapping it back to its original position so initial order is maintained
                swap(list,l,i);
            }
        }
    }

    //Swap function will swap ith and jth numbers of the list
    private void swap(List<Integer> list, int i, int j) {
        int temp=list.get(i);
        list.set(i,list.get(j));
        list.set(j,temp);
    }

}

Above code is used for generating permutation.

Udesh
  • 2,415
  • 2
  • 22
  • 32
  • 1
    Code without any explanation as to how it relates to the question is not as helpful. – Ruan Mendes Oct 27 '20 at 17:26
  • @JuanMendes I apologize I though OP could backtrack and understand it. Thanks for pointing it out. – Udesh Oct 28 '20 at 01:06
  • @VRB hope that this answer helps you. – Udesh Oct 28 '20 at 09:57
  • Yes, they often can, but it takes time through debugging. And it will also take time for everyone that reads your answers. If you take the time to write a summary of the problem, you'll be saving time for everyone. Also, the best way is typically an actual paragraph outside of the code explaining the problem and the solution in English. It's still unclear to me how this fixes the problem the OP posted. Their problem is that they were modifying an object and thinking they were working with a clone? – Ruan Mendes Oct 28 '20 at 17:22
  • @Juan I have comment it very well it needs some effort from OP side to back track and understand it.Ya I am working with the original list but in the second swap it bring it back to its original position so the order is maintained. – Udesh Oct 29 '20 at 00:44
  • Just explaining how to make a better answer, it's a moot point since the post was closed but it didn't look like you understood the OP's problem – Ruan Mendes Oct 29 '20 at 01:05
  • @Juan I realized my mistake whole time I was thinking that OP wants to generate permutation of a list now when I read it again I realized OP wanted to fix his bug. – Udesh Oct 29 '20 at 01:22