0

I wrote the following code for generating the permutations of given integers

import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        if (nums == null || nums.length == 0){
            return result;
        }
        boolean[] visited = new boolean[nums.length];
        List current = new ArrayList<Integer>();
        permute_recursive(visited, nums, current, result);
        return result;


    }
    public void permute_recursive(boolean[] visited, int[] nums, List<Integer> current, 
List<List<Integer>> result){
        if(current.size() == nums.length){
            result.add(new ArrayList<Integer>(current));
            return;
        }
        for(int i = 0; i < nums.length; i++){
            if(visited[i]){
                continue;
            }
            visited[i] = true;
            current.add(nums[i]);
            permute_recursive(visited, nums, current, result);
            visited[i] = false;
            //do not want the next round to start with nums[i], but we just added it, so we need to 
remove it.
            current.remove(current.size() - 1);


        }
    }
}

The problem is when I do result.add(current) directly it will only return to me an empty list of arrays and I do not know why.

As requested, my main method is

public class test {
    public static void main(String[] args) {
        int[] test = new int[]{1,2,3};
    System.out.print(Solution.permute(test));
    }
}

How is this relevant though?

Adam
  • 257
  • 3
  • 12
  • 1
    Include your main method. – LowKeyEnergy Nov 13 '19 at 21:32
  • Use debugger and see what happens. – Antoniossss Nov 13 '19 at 22:01
  • 1
    I couldn't see a problem here so I took your code and ran it - it worked perfectly and returned `[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]`. I have stylistic issues with it but algorithmically there's no issue. So I'm not exactly sure what you are asking. – sprinter Nov 13 '19 at 22:17
  • Your code also works fine for me. See it running here: https://ideone.com/wcUPaV – azurefrog Nov 13 '19 at 22:20
  • Did you guys change result.add(new ArrayList(current)) to result.add(current) while running? – Adam Nov 13 '19 at 23:11
  • The code I posted works fine but when I changed result.add(new ArrayList(current)) to result.add(current), it does not work. I wonder why. Sorry for the confusion. – Adam Nov 13 '19 at 23:15
  • @Adam result.add(new ArrayList(current)) creates current list copy. result.add(current) use the list object by reference. So i guess after adding current result you just call current.remove(current.size() - 1) which is actually cleans previously added elements. – Oleks Nov 18 '19 at 22:06
  • Aren‘t functions supposed to pass by value? – Adam Nov 19 '19 at 01:14
  • @Adam Yes - by value. In your case the "passed value" is the reference. In other words - for objects, the pass by value is the value of the reference to the object. https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Oleks Nov 19 '19 at 07:03
  • I see, that is tricky! Thank you very much! – Adam Nov 19 '19 at 21:38

0 Answers0