Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Cartesian product of two set?
dear all im working with this code to find Cartesian product of two set but it does not work this is the code
public static int[][] cartesianProducta(int[] B, int[] a2) {
int size1 = B.length;
int size2 = a2.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] = B[i];
result[d][1] = a2[j];
}
}
return result;
}
public static void main(String args[])
{
int[] B = {4, 2, 3};
int[] a2 = {2, 5};
System.out.println(cartesianProducta(B,a2));
3 Answers
- JohnLv 74 years ago
public static void main(String[] args) {
int[] a = {2, 5};
int[] b = {4, 2, 3};
System.out.println( Arrays.deepToString( cartesianProduct(a, b)));
//output: [[4, 2], [4, 5], [2, 2], [2, 5], [3, 2], [3, 5]]
}
private static int[][] cartesianProduct(int[] a, int[] b) {
int[][] result = new int[a.length * b.length][2];
for (int i = 0, d = 0; i < a.length; ++i) {
for (int j = 0; j < b.length; ++j, ++d) {
result[d][0] = a[i];
result[d][1] = b[j];
}
}
return result;
}
}
- husoskiLv 74 years ago
The logic looks fine, but Java won't print out anything useful when you print an array.
You can use Arrays.toString (from java.util.Arrays) to format an array for printing, but I think that will have the same problem with the inner int[2] arrays printing out with the generic toString() formatting inherited from Object. Try adding this before your public class:
class Pair {
.... public Pair(int x, int y) {
.... .... this.x = x;
.... .... this.y = y;
.... }
.... @Override
.... public String toString() {
.... .... return String.format("(%d, %d)", x, y);
.... public int x, y;
}
Be sure not to make that class public. Only one class in a .java source file can be public, and that's the class whose name matches the source filename. If you need Pair to be visible outside of the current package, you'll need to make it public and move it to its own Pair.java source file.
Next, change the return type of cartesianProduct to Pair[] instead of int[][]. Use new Pair(B[i], a2[j]) to create a Pair object representing those two numbers. The toString override will print out in (x, y) format, and then the single result array can be formatted with Arrays.toString(). That looks roughly like:
public static Pair[] cartesianProducta(int[] B, int[] a2) {
.... int size1 = B.length;
.... int size2 = a2.length;
.... Pair[] result = new Pair[size1 * size2];
.... for (int i = 0, d = 0; i < size1; ++i) {
.... .... for (int j = 0; j < size2; ++j, ++d) {
.... .... .... result[d] = new Pair(B[i], a2[j]);
.... .... }
.... }
.... return result;
}
public static void main(String args[]) {
.... int[] B = {4, 2, 3};
.... int[] a2 = {2, 5};
.... Pair[] result = cartesianProducta(B, a2);
.... System.out.println( Arrays.toString(result) );
}
The .... tokens are for visible indentation. Type spaces or tabs instead.
When processing the result with your own code, each entry in the result is a Pair object. You can loop through those with:
for (Pair p : result) {
.... System.out.print("(", p.x, ",", p.y, ") ");
}
System.out.println();
That is, if p is the Pair object, then the components are p.x and p.y. I didn't use OOP-style getter & setter methods for such a simple class, but it is nice to have the constructor fill in the values with new Pair(x, y) syntax.