私はこのようなコードを書いており、このメソッドには2つのリストが渡されます。tree list
は空のリストで、array list
はオブジェクトのリストです。なぜ正しい順列が返されないのですか?
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]
も行を継続しているが、私はここにちょうどそれらの一部をコピーしました! その問題は何ですか? 私 感謝 EDITを助けてください: これは文字列のためのコードであり、私は私のオブジェクトのためにそれを使用する:
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();
}
}
}
}
正確にあなたが達成したいですか?配列[1、2、3、4、5]を渡すと、どのような結果が期待されますか? –
**最初に**パラメータとして渡しました。つまり、 'beginningString'の初期値と' endingString'の初期値は何ですか? –
すべての順列が欲しい!例えば5つの数字の場合、私は5を欲しい!それぞれが配列リストとして表示される順列。 – user472221