fork download
  1. class Ideone
  2. {
  3. private static int[][][] cartesianProduct(int[] A, int[] B) {
  4. int width = B.length;
  5. int height = A.length;
  6. int[][][] result = new int[height][width][2];
  7. for (int ai = 0; ai < height; ai++) {
  8. for (int bi = 0; bi < width; bi++) {
  9. result[ai][bi][0] = A[ai];
  10. result[ai][bi][1] = B[bi];
  11. }
  12. }
  13. return result;
  14. }
  15.  
  16. public static void main (String[] args)
  17. {
  18. int[] B = {4, 2, 3};
  19. int[] a2 = {2, 5};
  20. int[][][] product = cartesianProduct(B, a2);
  21. System.out.println(productString(product));
  22. }
  23.  
  24. private static String productString(int[][][] p) {
  25. String s = "";
  26. for (int ai = 0; ai < p.length; ai++) {
  27. for (int bi = 0; bi < p[ai].length; bi++) {
  28. s += "(" + p[ai][bi][0] + "," + p[ai][bi][1] + ") ";
  29. }
  30. s += "\n";
  31. }
  32. return s;
  33. }
  34. }
Success #stdin #stdout 0.05s 4386816KB
stdin
Standard input is empty
stdout
(4,2) (4,5) 
(2,2) (2,5) 
(3,2) (3,5)