如何 return 给定数组在 Java 中的所有可能排列?

How can I return all possible permutations of a given array in Java?

假设我有嵌套数组

gridInterFT=[[1,2],[2,3],[3,4],[1,3],[1,4],[2,4]]

我将如何编写一个函数,将此数组作为输入并 returns 一个包含数组所有可能排列的 3d 嵌套数组? IE。返回的数组应该类似于:

[[[1,2],[2,3],[3,4],[1,3],[1,4],[2,4]], [[1,3],[2,3],[3,4],[1,2],[1,4],[2,4]], 
[[2,3],[1,2],[3,4],[1,3],[1,4],[2,4]]...]

是否有包含直接执行此操作的函数的库?

static <T> void swap(T[] a, int i, int j) {
    T t = a[i];
    a[i] = a[j];
    a[j] = t;
}

static void permutation(int[][] perm, int n, List<int[][]> result) {
    int size = perm.length;
    if (n >= size)
        result.add(Arrays.copyOf(perm, size));
    else
        for (int i = n; i < size; i++ ) {
            swap(perm, n, i);
            permutation(perm, n + 1, result);
            swap(perm, n, i);
        }
}
public static int[][][] permutation(int[][] perm) {
    List<int[][]> result = new ArrayList<>();
    permutation(perm, 0, result);
    return result.toArray(new int[0][][]);
}

public static void main(String[] args) {
    int[][] gridInterFT={{1,2},{2,3},{3,4},{1,3},{1,4},{2,4}};
    int[][][] r = permutation(gridInterFT);
    for (int[][] a : r) {
        for (int i = 0; i < a.length; ++i)
            System.out.print(Arrays.toString(a[i]));
        System.out.println();
    }
}