I am trying to create a cartesian product method in java that accepts sets as arguments and returns a set pair. The code I have coverts the argumented sets to arrays and then does the cartesian product but i can't add it back to the set pair that i want to return. Is there an easier way to do this? Thanks in advance.
public static <S, T> Set<Pair<S, T>> cartesianProduct(Set<S> a, Set<T> b) {
Set<Pair<S, T>> product = new HashSet<Pair<S, T>>();
String[] arrayA = new String[100];
String[] arrayB= new String[100];
a.toArray(arrayA);
b.toArray(arrayB);
for(int i = 0; i < a.size(); i++){
for(int j = 0; j < b.size(); j++){
product.add(arrayA[i],arrayB[j]);
}
}
return product;
}
` object to use as the argument to `product.add`. There is no `add` method for sets that takes two arguments. Also, I assume `arrayA` should be an array of `S`, not `String`... Also, you're not using `toArray` correctly; it will not change the length of the array if `a` is shorter than 100 and won't do anything useful if `a` is larger. Better is `arrayA = a.toArray(new S[0]);`, similarly for `b`.– ajb Feb 08 '14 at 00:54