Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.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 两个数组的笛卡尔积_Java_Arrays_Exception_Cartesian Product - Fatal编程技术网

Java 两个数组的笛卡尔积

Java 两个数组的笛卡尔积,java,arrays,exception,cartesian-product,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> lis

如何使用两个数组进行笛卡尔积

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;
}
public int[]cartesianProduct(int[]s1,int[]s2){
ArrayList=新建ArrayList();
对于(int i=0;i
输出数组的元素应该是整数对,因此
int
数组不能是输出的类型,并且不能使用
列表来收集数据

表示每对数字的一种方法是两个元素的整数数组。这样,输出将是2D数组,用于收集数据的
列表可以是
列表

public int[]cartesianProduct(int[]s1,int[]s2){
列表=新的ArrayList();
对于(int v1:s1){
对于(int v2:s2){
添加(新的int[]{v1,v2});
}
}
int[][]结果=新的int[list.size()][2];
int k=0;
for(int[]i:list){
结果[k++]=i;
}   
返回结果;
}

作为@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,int[]s2){
列表=新的ArrayList();
对于(int v1:s1){
对于(int v2:s2){
添加(新的int[]{v1,v2});
}
}
退货清单;
}
要获得所需的输出,只需按如下所示迭代列表

用法:

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("}");
int[]s1=新的int[]{1,2,3};
int[]s2=新的int[]{2,3,4};
列表=cartesianProduct(s1,s2);
系统输出打印(“{”);
对于(int i=0;i0)系统输出打印(“,”);
系统输出打印(“+list.get(i)[0]+”,“+list.get(i)[1]+”);
}
System.out.println(“}”);

您不需要
列表
。试试这个

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;
}
publicstaticint[]cartesianProduct(int[]s1,int[]s2){
int size1=s1.1长度;
int size2=s2.5长度;
int[][]结果=新int[size1*size2][2];
对于(int i=0,d=0;i
使用
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]]

n个数组的递归解决方案:使用param 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;
}
public List cartesianProduct(inti,List…a){
如果(i==a.length){
列表结果=新建ArrayList();
添加(新的ArrayList());
返回结果;
}
下一个列表=卡特尔产品(i+1,a);
列表结果=新建ArrayList();
对于(int j=0;j
//另外,使用int[][]输入时效果很好,返回int[][]
公共静态int[][]cartesianProduct(int[]s1,int[]s2){
int size1=s1.1长度;
int size2=s2.5长度;
int[][]结果=新的int[size1*size2][2][2];
对于(int i=0,d=0;i
list.add(s1[i],s2[i]),你希望得到什么要做什么?笛卡尔积要使用什么表示法?目前,您没有表示一对数字的任何内容。我希望如上所述将值分配给ArrayList,并如上所述打印该列表。但这种方法不起作用。v1和v2从未使用过。你没有收到警告吗?为什么不呢?当我打印出来时,我会得到一个奇怪的输出。@MichaelLee,这可能是因为数组没有覆盖对象的toString。尝试打印数组。deepToString(结果)
。谢谢,deepToString有效,但这是我现在得到的输出[[1,2]、[1,2]、[1,2]、[1,2]、[1,2]、[1,2]、[1,2]、[1,2]、[2,3]、[2,3]、[2,3]、[2,3]、[2,3]、[3,4]、[3,4]、[3,4]
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;
}
    //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;
}