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