anyone can help me..import java.util.Scanner;
import java.io.PrintStream;
import java.util.ArrayList;
public class Subset {
public static void main(String[] args) {
// Initial set: it may contains repeated elements
int members[]={1,2,3};
// The actual set
ArrayList<Integer> set = new ArrayList<Integer>();
// The repeated elements would not be inserted in the actual set.
for (int n : members) {
if (!set.contains(n)) {
set.add(n);
}
}
// Dump the set itself
System.out.print("Set: ");
dump(System.out, set);
// extracts the subset
ArrayList<ArrayList<Integer>> subSets = getSubsets(set);
System.out.println("Subsets:");
// dump the subsets one by one
for (ArrayList<Integer> subset : subSets) {
dump(System.out, subset);
}
}
private static ArrayList<ArrayList<Integer>> getSubsets(
ArrayList<Integer> set) {
ArrayList<ArrayList<Integer>> subsetCollection = new ArrayList<ArrayList<Integer>>();
if (set.size() == 0) {
subsetCollection.add(new ArrayList<Integer>());
} else {
ArrayList<Integer> reducedSet = new ArrayList<Integer>();
reducedSet.addAll(set);
int first = reducedSet.remove(0);
ArrayList<ArrayList<Integer>> subsets = getSubsets(reducedSet);
subsetCollection.addAll(subsets);
subsets = getSubsets(reducedSet);
for (ArrayList<Integer> subset : subsets) {
subset.add(0, first);
}
subsetCollection.addAll(subsets);
}
return subsetCollection;
}
// This method dumps the elements of a set in one line
// It dumps the set into the given PrintStream object.
private static void dump(PrintStream out, ArrayList<Integer> set) {
out.print("{");
boolean first = true;
for (int n : set) {
if (first) {
first = false;
} else {
out.print(", ");
}
out.printf("%1$d", n);
}
out.println("}");
}
}
Set: {1, 2, 3}
Subsets:
{}
{3}
{2}
{2, 3}
{1}
{1, 3}
{1, 2}
{1, 2, 3}
What i want is :
{}
{1}
{2}
{1, 2}
{3}
{1, 3}
{2, 3}
{1, 2, 3}
also i want I will be the one to input the numbers,, like having a Scanner.. and lastly also i want to get a subset of String like {bad,good,best better}.. i really appreciate all your help its for our project in algebra but arraylist is still not thought to us in programming class.. so i cant fully understand the code above.. also i dont own the code above i just copied it from my classmate which he also copied from the net.