'List of List not retaining the contents

so I have a recursive program where I am trying to generate all the permutations of a String. I intend to store the permutations in a list of list called ans.

Every single current permutation is stored in the container list which is used to populate the ans list. I am suspecting , since list is a reference type so maybe I am loosing the values in ans list because the container list is getting manipulated? Am I an idiot



import java.util.*;
public class practice_3 {
   static String str="abc";
   static List<List<Character>> ans=new ArrayList<List<Character>>();

   
public static void permString(ArrayList container )
{
    if(container.size()==str.length())
        {System.out.println(container);
        
        ans.add(container);
        return;
        }
    
    for(int i=0;i<str.length();i++)
    {
        if(!container.contains(str.charAt(i)))
        {
            container.add(str.charAt(i));
            permString(container);
            container.remove(container.size()-1);
            
        }
            
    }
    
}
public static void main(String[] args) {
    
    ArrayList<Character> container=new ArrayList<>();
    
    
    permString(container);
    System.out.println(ans);
    System.out.println("container end="+container);
    
    
}

}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source