2010-12-10 12 views
-1

私はこのようなコードを書いており、このメソッドには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(); 
     } 
     } 
    } 
} 
+0

正確にあなたが達成したいですか?配列[1、2、3、4、5]を渡すと、どのような結果が期待されますか? –

+0

**最初に**パラメータとして渡しました。つまり、 'beginningString'の初期値と' endingString'の初期値は何ですか? –

+0

すべての順列が欲しい!例えば5つの数字の場合、私は5を欲しい!それぞれが配列リストとして表示される順列。 – user472221

答えて

1

は、以下のコードを試してみてください。私は単に文字列のために働くpermuteメソッドを取って、それを(ほとんどの行単位で)どんな種類のリストでも動作するものに変換しました。

public static <E> void permute2(List<E> beginningList, List<E> endingList) { 
    if (endingList.size() <= 1) { 
     //combine the two lists 
     List<E> result = new ArrayList<E>(beginningList); 
     result.addAll(endingList); 
     System.out.println(result); 
    } else 
     for (int i = 0; i < endingList.size(); i++) { 

      //create a list without the ith element 
      List<E> newList = new ArrayList<E>(endingList); 
      newList.remove(i); 

      //create a list by adding the ith element to beginning 
      List<E> newBeginning = new ArrayList<E>(beginningList); 
      newBeginning.add(endingList.get(i)); 

      permute2(newBeginning, newList); 
     } 
} 

例:

public static void main(String args[]) { 
    permute2(new ArrayList<Integer>(), new ArrayList<Integer>(Arrays.asList(1, 2, 3))); 
} 

プリント:

[1, 2, 3] 
[1, 3, 2] 
[2, 1, 3] 
[2, 3, 1] 
[3, 1, 2] 
[3, 2, 1] 
+0

あなたは正しいです、答えをありがとう、私は私の本当の間違いを見つけることを考えています! – user472221