Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么';t返回正确的排列!_Java_Permutation - Fatal编程技术网

Java 为什么';t返回正确的排列!

Java 为什么';t返回正确的排列!,java,permutation,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); } Syst

我在下面编写了这样一个代码,我将向这个方法传递两个列表,
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();
            }
        }
    }

}
还有一些行是继续的,但我在这里复制了其中的一些! 它有什么问题? 请帮帮我 谢谢 编辑: 这是字符串的代码,我将其用于我的对象:

  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();
        }
      }
  }
}
import java.io.*;
公共类新类{
公共静态void main(字符串args[])引发IOException{
字符串str;
System.out.println(“输入初始字符串”);
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
str=br.readLine();
System.out.println(“排列为:”);
排列(“,str);
}
公共静态void排列(字符串开始字符串、字符串结束字符串){

如果(endingString.length(),请尝试下面的代码。我只是简单地使用了适用于字符串的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);
        }
}

您到底想要实现什么?如果通过数组[1,2,3,4,5],您希望得到什么结果?最初作为参数传递的是什么,即
beginingstring
的初始值和
endingString
的初始值是多少?我想要它的所有排列!例如,对于5个数字,我想要5个!排列,每个排列将显示为一个数组列表。那么,为什么传递两个数组?您的代码甚至没有编译。我需要什么s
列表
?你说得对,谢谢你的回答,我在想我是否发现了真正的错误!
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]