1

I have a permutation recursive array which works fine but I need to store the result not just print them out,I have to store each print out in a separate array or the whole in one array

here is the original:

public static void Permute(String soFar, String rest)  
        {  
           if (rest.isEmpty())  
           {
               System.out.println(soFar);  

           }           
            else  
           {  
               for (int i =0; i < rest.length(); i++)  
                {  
                    String next = soFar + rest.charAt(i);  
                    String remaining  = rest.substring(0,i) + rest.substring(i+1);  
                    Permute(next,remaining);  
                }  
           }  
        }  

and I changed it but the problem is that return arr is not proper in there I should do something else because the arr would become empty because other recursive function would be called and I don't want it

public static String Permute(String soFar, String rest,String arr)  
    {  
       if (rest.isEmpty())  
       {
        //   System.out.println(soFar);  
       arr+=soFar;
       }           
        else  
       {  
           for (int i =0; i < rest.length(); i++)  
            {  
                String next = soFar + rest.charAt(i);  
                String remaining  = rest.substring(0,i) + rest.substring(i+1);  
                Permute(next,remaining,arr);  
            }  
       }  
    return arr;
    }  
Nickool
  • 3,662
  • 10
  • 42
  • 72

2 Answers2

1

One way;

  1. Create an instance variable of type List: List<String> myList = new ArrayList<String>();
  2. Replace System.out.println(soFar); with myList.add(soFar);
  3. Probably in the end convert your list to an array: String[] arr = (String[]) myList.toArray();
StoopidDonut
  • 8,547
  • 2
  • 33
  • 51
  • where should I create the list? if I create it in the first line every time it will be renewed? – Nickool May 10 '14 at 06:37
  • You can create it as an [instance (class level) variable](http://stackoverflow.com/questions/16686488/java-what-is-an-instance-variable) so that it's just instantiated once – StoopidDonut May 10 '14 at 06:38
1

Change your arr parameter to be a List. Then instead of the println() just keep adding to this List. Also, when you call permute for the first time, make sure you pass in an allocated List. Like so:

public void ParentFunction()
{
    List<String> results = new ArrayList<String>();
    Permute(..., ..., results);
    // Now you have all results inside "results" variable.
}

public static void Permute(String soFar, String rest, List<String> results)  
{
    if (rest.isEmpty())  
    {
        //System.out.println(soFar);  
        results.add(soFar); // ADD TO RESULT LIST
    }           
    else  
    {  
        for (int i = 0; i < rest.length(); i++)  
        {  
            String next = soFar + rest.charAt(i);  
            String remaining  = rest.substring(0, i) + rest.substring(i + 1);  
            Permute(next, remaining, results);  
        }  
    }
} 
metacubed
  • 7,031
  • 6
  • 36
  • 65
  • Thank you buddy,quick neat explained – Nickool May 10 '14 at 06:58
  • @Nickparsa You should use the least specific interface necessary to accept a parameter or declare a variable. Your edit for the `new ArrayList` part was correct, but otherwise, your edits introduced bad practices. – Corbin May 10 '14 at 07:07
  • @Corbin it gave me error when I used his so I changed it if you know how to edit please do it so everybody will see the correct one – Nickool May 10 '14 at 07:13
  • 1
    @Nickparsa Just the `new ArrayList()` needed to be changed - the rest can remain as `List`. I've updated your update :) – metacubed May 10 '14 at 07:17