Я написал такой код ниже, и я передам два списка этому методу, который 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]
также строки продолжаются, но я скопировал здесь только некоторые из них!в чем его проблема?Пожалуйста, помогите мне спасибо. РЕДАКТИРОВАТЬ: Это код для строк, и я использовал его для своих объектов:
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();
}
}
}
}