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;
    }  
1
  • Can you not use a List? Commented May 10, 2014 at 6:27

2 Answers 2

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);  
        }  
    }
} 
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you buddy,quick neat explained
@Nickparsa You should use the least specific interface necessary to accept a parameter or declare a variable. Your edit for the new ArrayList<String> part was correct, but otherwise, your edits introduced bad practices.
@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
@Nickparsa Just the new ArrayList<String>() needed to be changed - the rest can remain as List. I've updated your update :)
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();

2 Comments

where should I create the list? if I create it in the first line every time it will be renewed?
You can create it as an instance (class level) variable so that it's just instantiated once

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.