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

发布于 2024-10-07 10:33:18 字数 4028 浏览 2 评论 0原文

我在下面编写了这样的代码,我将向此方法传递两个列表,其中树列表是空列表,数组列表是对象列表。

    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();
        }
      }
  }
}

I Have written such a code below and I will pass two lists to this method which tree list is empty list and array list is a list of objects.

    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();
            }
        }
    }

}

but it will print the lines below:

[digit :7  probability: 66.0, digit :7  probability: 66.0]
[digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0]
[digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0]
[digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0]
[digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0]
[digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0]
[digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0]

also lines are continued but I copied here just some of them!
what is its problem?
please help me
thanks
EDIT:
This is the code for Strings and I used it for my objects :

  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();
        }
      }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他不在意 2024-10-14 10:33:18

尝试下面的代码。我只是简单地采用了适用于字符串的排列方法,并将其(几乎逐行)转换为适用于任何类型列表的方法。

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]

Try the code below. I have simply taken your permute method which works for strings, and translated it (almost line-by-line) into one which should work for any kind of list.

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);
        }
}

Example:

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

Prints:

[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文