Variable 'result' is blank when printed inside main method. Can someone guide me how to structure the code? New to java. apologies if the Q is naive.
import java.util.ArrayList;
import java.util.List;
public class StringPermutation {
public static void main(String[] args){
int[] a = new int[]{1,2,3};
System.out.println(permute(a));
}
public static List<List<Integer>> permute(int[] a) {
List<Integer> path = new ArrayList<>();
List<List<Integer>> result = new ArrayList(path);
boolean[] visited = new boolean[a.length];
helper(result, path, visited, a);
//System.out.println(result);
return result;
}
private static void helper(List<List<Integer>> result, List<Integer> path, boolean[] visited, int[] a) {
if (path.size() == a.length)
result.add(path);
for (int i = 0; i < a.length; i++) {
if (visited[i]) continue;
path.add(a[i]);
visited[i] = true;
helper(result, path, visited, a );
path.remove(path.size() - 1);
visited[i] = false;
}
}
}
> result = new ArrayList(path)` doesn't do what you (probably) think it does, and also gives a warning about using raw types.
– Mick Mnemonic Jan 07 '18 at 22:09