1
import javax.swing.JOptionPane;
public class Permutations {

public static void main(String[] args) throws Exception {
    String str = null;
    str = JOptionPane.showInputDialog("Enter a word");
    StringBuffer strBuf = new StringBuffer(str);
    doPerm(strBuf,str.length());

}

private static void doPerm(StringBuffer str, int index){
    String[] anArrayOfStrings;  
    if(index == 0){ 
        System.out.println(str);
        }      
    else { 
        doPerm(str, index-1);
        int currPos = str.length()-index;
        for (int i = currPos+1; i < str.length(); i++) {
            swap(str,currPos, i);
            doPerm(str, index-1);
            swap(str,i, currPos);
        } 
    }       

}


private  static void swap(StringBuffer str, int pos1, int pos2){
    char t1 = str.charAt(pos1);
    str.setCharAt(pos1, str.charAt(pos2));
    str.setCharAt(pos2, t1);
} 

}

使用上面的代码,我排列一个单词并在控制台中打印它们。

样本输入:不好

输出:

bad
bda
abd
adb
dab
dba

我想在 JOptionPane 中显示输出。我试图替换这条线

System.out.println(str);

有了这个

JOptionPane.showMessageDialog(null, str);

但是所有输出都没有加载到 1 个 JOptionPane 中。相反,它向我显示一个带有“坏”的 JOptionPane,当我单击确定或按 Enter 时,将显示一个带有“bda”的 JOptionPane,依此类推,直到它完成循环。我想要的是在单个 JOptionPane 中显示 6 个输出。

我也尝试像数组但几乎相同的输出。

    private static void doPerm(StringBuffer str, int index){
    ArrayList<String> list = new ArrayList<String>();
    if(index == 0){ 
        list.add(str.toString());
        }      
    else { 
        doPerm(str, index-1);
        int currPos = str.length()-index;
        for (int i = currPos+1; i < str.length(); i++) { 
            swap(str,currPos, i);
            doPerm(str, index-1);
            swap(str,i, currPos);
        } 
    }
    JOptionPane.showMessageDialog(null, list);

}
4

2 回答 2

2

您正在使用递归。您不想每次调用 doPerm() 方法时都创建一个新的 ArrayList。相反,您在方法之外创建 ArrayList 并在每次调用时将 ArrayList 传递给方法。就像是:

String str = null;
str = JOptionPane.showInputDialog("Enter a word");
StringBuffer strBuf = new StringBuffer(str);
List<String> list = new ArrayList<String>();
//doPerm(strBuf,str.length());
doPerm(list, strBuf,str.length());
System.out.println(list);
//JOptionPane.showMessageDialog(null, list);


//private static void doPerm(StringBuffer str, int index)
private static void doPerm(List, list, StringBuffer str, int index)
{
    //ArrayList<String> list = new ArrayList<String>();
    if(index == 0){ 
        list.add(str.toString());
        }      
    else { 
        //doPerm(str, index-1);
        doPerm(list, str, index-1);
        int currPos = str.length()-index;
        for (int i = currPos+1; i < str.length(); i++) { 
            swap(str,currPos, i);
            //doPerm(str, index-1);
            doPerm(list, str, index-1);
            swap(str,i, currPos);
        } 
    }
于 2013-07-01T18:05:31.660 回答
2

我猜你正在寻找这样的东西:
在此处输入图像描述
你应该按如下方式进行:

import javax.swing.JOptionPane;
public class Permutation {

public static void main(String[] args) throws Exception {
    String str = null;
    str = JOptionPane.showInputDialog("Enter a word");
    StringBuffer strBuf = new StringBuffer(str);
    doPerm(strBuf,str.length());
    JOptionPane.showMessageDialog(null,sbuf.toString());
}
static StringBuffer sbuf = new StringBuffer();
private static void doPerm(StringBuffer str, int index){
    String[] anArrayOfStrings;  
    if(index == 0){ 
        //System.out.println(str);
        sbuf.append(str+"\n");
        }      
    else { 
        doPerm(str, index-1);
        int currPos = str.length()-index;
        for (int i = currPos+1; i < str.length(); i++) {
            swap(str,currPos, i);

            doPerm(str, index-1);
            swap(str,i, currPos);
        } 
    }       

}


private  static void swap(StringBuffer str, int pos1, int pos2){
    char t1 = str.charAt(pos1);
    str.setCharAt(pos1, str.charAt(pos2));
    str.setCharAt(pos2, t1);
} 

}
于 2013-07-01T18:13:01.033 回答