为什么不返回正确的排列!

时间:2010-12-10 11:11:31

标签: java permutation

我已经在下面编写了这样的代码,我将向此方法传递两个列表,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]

也继续行,但我复制了其中一些! 它的问题是什么? 请帮我 谢谢 编辑: 这是Strings的代码,我将它用于我的对象:

  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 个答案:

答案 0 :(得分: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]