J'ai écrit un tel code ci-dessous et je vais passer deux listes à cette méthode qui tree list
est la liste vide et array list
est une liste d'objets.pourquoi ne retourne pas les permutations correctes!
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();
}
}
}
}
mais il imprimera les lignes ci-dessous:
[digit :7 probability: 66.0, digit :7 probability: 66.0]
[digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0]
[digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0]
[digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0]
[digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0]
[digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0]
[digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0, digit :7 probability: 66.0]
aussi les lignes sont poursuivies, mais je ici que quelques-uns copiés d'entre eux! quel est son problème? s'il vous plaît aidez-moi grâce EDIT: Voici le code pour cordes et je l'ai utilisé pour mes objets:
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();
}
}
}
}
Que voulez-voulez-vous atteindre? Quel résultat attendez-vous si vous passez le tableau [1, 2, 3, 4, 5]? –
Qu'avez-vous ** initialement ** transmis en tant que paramètres, c'est-à-dire Quelle est la valeur initiale de 'beginString' et la valeur initiale de' endingString'? –
Je veux toutes ses permutations! par exemple pour 5 chiffres, j'en veux 5! permutations que chacun sera montré comme une liste de tableau. – user472221