为什么不返回正确的排列!
我在下面编写了这样的代码,我将向此方法传递两个列表,其中树列表
是空列表,数组列表
是对象列表。
public void permute(List<Element> tree, List<Element> array) {
if (array.size() <= 1) {
for (Element i : array) {
tree.add(i);
}
System.out.println(tree.toString());
} else {
for (int i = 0; i < array.size(); i++) {
try {
list = array.subList(0, i);
list.add(array.get(i + 1));
tree.add(array.get(i));
permute(tree, list);
} catch (StringIndexOutOfBoundsException exception) {
exception.printStackTrace();
}
}
}
}
但它会打印下面的行:
[digit :7 probability: 66.0, digit :7 probability: 66.0]
[digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0]
[digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0]
[digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0]
[digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0]
[digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0]
[digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0]
也继续行,但我在这里复制了其中的一些! 它的问题是什么? 请帮我 谢谢 编辑: 这是字符串的代码,我将其用于我的对象:
import java.io.*;
public class NewClass{
public static void main(String args[]) throws IOException{
String str;
System.out.println("Enter the initial string");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
str=br.readLine();
System.out.println("Permutations are :");
permute("", str);
}
public static void permute(String beginningString, String endingString) {
if (endingString.length() <= 1)
System.out.println(beginningString + endingString);
else
for (int i = 0; i < endingString.length(); i++) {
try {
String newString = endingString.substring(0, i) + endingString.substring(i + 1);
permute(beginningString + endingString.charAt(i), newString);
} catch (StringIndexOutOfBoundsException exception) {
exception.printStackTrace();
}
}
}
}
I Have written such a code below and I will pass two lists to this method which tree list
is empty list and array list
is a list of objects.
public void permute(List<Element> tree, List<Element> array) {
if (array.size() <= 1) {
for (Element i : array) {
tree.add(i);
}
System.out.println(tree.toString());
} else {
for (int i = 0; i < array.size(); i++) {
try {
list = array.subList(0, i);
list.add(array.get(i + 1));
tree.add(array.get(i));
permute(tree, list);
} catch (StringIndexOutOfBoundsException exception) {
exception.printStackTrace();
}
}
}
}
but it will print the lines below:
[digit :7 probability: 66.0, digit :7 probability: 66.0]
[digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0]
[digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0]
[digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0]
[digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0]
[digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0]
[digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0]
also lines are continued but I copied here just some of them!
what is its problem?
please help me
thanks
EDIT:
This is the code for Strings and I used it for my objects :
import java.io.*;
public class NewClass{
public static void main(String args[]) throws IOException{
String str;
System.out.println("Enter the initial string");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
str=br.readLine();
System.out.println("Permutations are :");
permute("", str);
}
public static void permute(String beginningString, String endingString) {
if (endingString.length() <= 1)
System.out.println(beginningString + endingString);
else
for (int i = 0; i < endingString.length(); i++) {
try {
String newString = endingString.substring(0, i) + endingString.substring(i + 1);
permute(beginningString + endingString.charAt(i), newString);
} catch (StringIndexOutOfBoundsException exception) {
exception.printStackTrace();
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试下面的代码。我只是简单地采用了适用于字符串的排列方法,并将其(几乎逐行)转换为适用于任何类型列表的方法。
示例:
打印:
Try the code below. I have simply taken your permute method which works for strings, and translated it (almost line-by-line) into one which should work for any kind of list.
Example:
Prints: