0

I hope this isn't a stupid question but I took the code below from another post. It just generates all permutations of a string. What I'd like to do is just modify it so all the permutations are added to an arraylist but I'm having some trouble finding what is hopefully the simple obvious solution. Can someone give this a quick look and explain what I'm doing wrong? I just want to take the permutations of a string and create an array list, that's all.

public class UserInput {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter Word: ");
    List<String> inputList = new ArrayList<String>();
    String input = scan.next();
    permutation(input);

            //Error occurs here
    inputList.addAll(permutation(input));

}

public  static void permutation(String str) { 
    permutation("", str);
 }

 private static void permutation(String prefix, String str) {   
    int n = str.length();
    if (n == 0) System.out.println(prefix);
    else {
        for (int i = 0; i < n; i++)
           permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n));
    }
}

}

3 Answers 3

6

Permutation has no return type, it is a void method, you are putting it in a list which only accepts objects of type String. You can modify it so once the recursion reaches the lowest level it adds itself to the list like so:

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter Word: ");
    List<String> inputList = new ArrayList<String>();
    String input = scan.next();
    permutation(input, inputList);

    System.out.println(inputList);

}

public static void permutation(String str, List<String> result) {
    permutation("", str, result);
}

private static void permutation(String prefix, String str,
        List<String> container) {
    int n = str.length();
    if (n == 0) {
        container.add(prefix);
    } else {
        for (int i = 0; i < n; i++)
            permutation(prefix + str.charAt(i),
                    str.substring(0, i) + str.substring(i + 1, n),
                    container);
    }
}

For "Hei"

[Hei, Hie, eHi, eiH, iHe, ieH]
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of printing the permutation out, you can add it to the list.

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

public class UserInput {
private static List<String> inputList;
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter Word: ");
    inputList = new ArrayList<>();
    String input = scan.next();
    permutation(input);

   System.out.println(inputList.toString());

}

public  static void permutation(String str) { 
    permutation("", str);
 }

 private static void permutation(String prefix, String str) {   
    int n = str.length();
    if (n == 0) inputList.add(prefix);
    else {
        for (int i = 0; i < n; i++)
           permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n));
    }
}
}

Comments

-1

permutation(input) returns void, while inputList.addAll() expects Collection<String>.

Comments

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.