actually i want to write all subsets of a given set in this way :
for example if my set is A:{1,2} i want to have {} , {1} , {2} , {1,2}
this is what i tried :
static void printSubsets(java.util.Set<Integer> a) {
int n = a.size();
// Run a loop for printing all 2^n
// subset one by one
for (int i = 0; i < (1 << n); i++) {
System.out.print("{");
// Print current subset
for (int j = 0; j < n; j++) {
// (1<<j) is a number with jth bit 1
// so when we 'and' them with the
// subset number we get which numbers
// are present in the subset and which
// are not
if ((i & (1 << j)) > 0)
System.out.print(a.toArray()[j] + ",");
if (j == n - 1)
System.out.print(a.toArray()[j]);
}
System.out.print("} , ");
}
}
and this is the out put {} , {1,} , {2,} , {1,2,} , my problem is with , . i do not want to have , at the end of every subset and at the end of the whole output can you help me solve this problem to have a output like {} , {1} , {2} , {1,2}
Aucun commentaire:
Enregistrer un commentaire