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,当我单击OK或按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);
}
答案 0 :(得分: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);
}
}
答案 1 :(得分: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);
}
}