两个阵列的笛卡尔积

时间:2016-10-23 10:44:21

标签: java arrays exception cartesian-product

如何使用两个阵列来制作笛卡尔积?

A = {1, 2, 3} 
B = {2, 3, 4}
C = {(1, 2), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 2), (3, 3), (3, 4)}.

这是我正在使用的代码,但我总是得到一个IndexOutOfBoundException。

public int[] cartesianProduct(int[] s1, int[] s2) {

    ArrayList<Integer> list = new ArrayList<>();
    for(int i=0;i < s1.length;i++){
    for (int v1: s1) {
        for (int v2: s2) {
            list.add(s1[i], s2[i]);
            }
        }
    }
        int[] result = new int[list.size()];
        int k=0;
        for(int i: list){
            result[k++] = i;
        }   
        return result;
}

6 个答案:

答案 0 :(得分:3)

使用java8获取Cartesian产品

    int[] A = { 1, 2, 3 };
    int[] B = { 2, 3, 4 };
    int[][] AB = Arrays.stream(A).boxed().flatMap(ai -> Arrays.stream(B).boxed().map(bi -> new int[] { ai, bi })).toArray(int[][]::new);
    System.out.println("cartesian " + Arrays.deepToString(AB));

输出

cartesian [[1, 2], [1, 3], [1, 4], [2, 2], [2, 3], [2, 4], [3, 2], [3, 3], [3, 4]]

答案 1 :(得分:1)

输出数组的元素应该是整数对,因此<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app"> <div ng-controller="Ctrl as myCtrl" class="btn-group pull-right"> <button ng-class="{active: myCtrl.onactive}" class="btn" ng-click="myCtrl.changeColorIcons('on')">on</button> <button ng-class="{active: myCtrl.offactive}" class="btn" ng-click="myCtrl.changeColorIcons('off')">off</button> </div> </div>数组不能是输出的类型,并且您不能使用int来收集数据。

表示每对数字的一种方法是两个元素的int数组。这样输出就是2D数组,用于收集数据的List<Integer>可以是List

List<int[]>

答案 2 :(得分:0)

作为@Eran给出的答案的替代方案,我将停止拥有有序对的列表:

public List<int[]> cartesianProduct(int[] s1, int[] s2) {
    List<int[]> list = new ArrayList<int[]>();
    for (int v1: s1) {
        for (int v2: s2) {
            list.add(new int[]{v1, v2});
        }
    }

    return list;
}

要获得所需的输出,您可以按如下方式迭代列表。

<强>用法:

int[] s1 = new int[] {1, 2, 3};
int[] s2 = new int[] {2, 3, 4};
List<int[]> list = cartesianProduct(s1, s2);
System.out.print("{");
for (int i=0; i < list.size(); ++i) {
    if (i > 0) System.out.print(", ");
    System.out.print("(" + list.get(i)[0] + ", " + list.get(i)[1] + ")");
}
System.out.println("}");

答案 3 :(得分:0)

您不需要List。试试这个。

public static int[][] cartesianProduct(int[] s1, int[] s2) {
    int size1 = s1.length;
    int size2 = s2.length;
    int[][] result = new int[size1 * size2][2];
    for (int i = 0, d = 0; i < size1; ++i) {
        for (int j = 0; j < size2; ++j, ++d) {
            result[d][0] = s1[i];
            result[d][1] = s2[j];
        }
    }
    return result;
}

答案 4 :(得分:0)

n个数组的递归解决方案:以参数i = 0调用

public <T> List<List<T>> cartesianProduct(int i, List<T>... a) {
    if(i == a.length ) {
        List<List<T>> result = new ArrayList<>();
        result.add(new ArrayList());
        return result;
    }
    List<List<T>> next = cartesianProduct(i+1, a);
    List<List<T>> result = new ArrayList<>();
    for(int j=0; j < a[i].size(); j++) {
        for(int k=0; k < next.size(); k++) {
            List<T> concat = new ArrayList();
            concat.add(a[i].get(j));
            concat.addAll(next.get(k));
            result.add(concat);
        }
    }
    return result;
}

答案 5 :(得分:0)

    //Also, works great with int[][] input, returning int[][][]
    public static int[][][] cartesianProduct(int[][] s1, int[][] s2) {
    int size1 = s1.length;
    int size2 = s2.length;
    int[][][] result = new int[size1 * size2][2][2];
    for (int i = 0, d = 0; i < size1; ++i) {
        for (int j = 0; j < size2; ++j, ++d) {
            result[d][0] = s1[i];
            result[d][1] = s2[j];
        }
    }
    return result;
}