Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Semrush - keyword research tool

Linux Foundation Training and Certification

Teach Your Child To Read

Disclosure: My content contains affiliate links.

32,304 questions

42,479 answers

573 users

How to perform the cartesian product of two arrays in Java

3 Answers

0 votes
import java.util.Arrays;
import java.util.List;

public class PerformCartesianProductOfTwoArrays_Java {
    public static void main(String[] args) {
        List<String> arr_str = Arrays.asList("c", "java", "python", "rust", "c#");
        List<Integer> arr_num = Arrays.asList(1, 2, 3, 4);

        arr_str.stream()
               .flatMap(f -> arr_num.stream().map(a -> new Pair(f, a)))
               .forEach(r -> System.out.println(r.language + " " + r.number));
    }
}

class Pair {
    String language;
    int number;

    Pair(String language, int number) {
        this.language = language;
        this.number = number;
    }
}

 
 
/*
run:

c 1
c 2
c 3
c 4
java 1
java 2
java 3
java 4
python 1
python 2
python 3
python 4
rust 1
rust 2
rust 3
rust 4
c# 1
c# 2
c# 3
c# 4
   
*/

 



Learn & Practice Python
with the most comprehensive set of 13 hands-on online Python courses
Start now


answered 5 days ago by avibootz
0 votes
import java.util.Arrays;

public class PerformCartesianProductOfTwoArrays_Java {
    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3};
        int[] arr2 = {6, 7, 8, 9};
        
        int[][] result = Arrays.stream(arr1).boxed()
                        .flatMap(ai -> Arrays.stream(arr2).boxed()
                        .map(bi -> new int[]{ai, bi}))
                        .toArray(int[][]::new);
                        
        System.out.println("Cartesian product:\n" + Arrays.deepToString(result));
    }
}

 
/*
run:

Cartesian product:
[[1, 6], [1, 7], [1, 8], [1, 9], [2, 6], [2, 7], [2, 8], [2, 9], [3, 6], [3, 7], [3, 8], [3, 9]]
   
*/

 



Learn & Practice Python
with the most comprehensive set of 13 hands-on online Python courses
Start now


answered 5 days ago by avibootz
0 votes
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;

public class PerformCartesianProductOfTwoArrays_Java {
    public static int[][] cartesianProduct(int[] arr1, int[] arr2) {
        List<int[]> list = new ArrayList<>();
        
        for (int val1 : arr1) {
            for (int val2 : arr2) {
                list.add(new int[]{val1, val2});
            }
        }
        
        int[][] result = new int[list.size()][2];
        int i = 0;
        for (int[] val : list) {
            result[i++] = val;
        }
        
        return result;
    }
    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3};
        int[] arr2 = {6, 7, 8, 9};

        System.out.println("Cartesian product:\n" + Arrays.deepToString(cartesianProduct(arr1, arr2)));
    }
}

 
/*
run:

Cartesian product:
[[1, 6], [1, 7], [1, 8], [1, 9], [2, 6], [2, 7], [2, 8], [2, 9], [3, 6], [3, 7], [3, 8], [3, 9]]
   
*/

 



Learn & Practice Python
with the most comprehensive set of 13 hands-on online Python courses
Start now


answered 5 days ago by avibootz

Related questions

2 answers 52 views
1 answer 49 views
1 answer 48 views
...