Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Request:Generating Subsets

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Request:Generating Subsets

    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.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Request:Generating Subsets

    Post your code here with [ code ] before and [ /code ] after (without spaces). Post the exact error message your get (if you get them). Ask a specific question.
    Improving the world one idiot at a time!

Similar Threads

  1. Password Generating Program Help
    By iAce in forum Java Theory & Questions
    Replies: 7
    Last Post: December 22nd, 2012, 09:00 PM
  2. [SOLVED] generating Dewey ID using Java
    By Rola in forum Object Oriented Programming
    Replies: 2
    Last Post: October 9th, 2012, 02:29 PM
  3. [SOLVED] Help Generating a level
    By Montario in forum AWT / Java Swing
    Replies: 22
    Last Post: April 12th, 2012, 07:22 AM
  4. Reading file of floats into array list and sorting into subsets
    By Skave in forum Collections and Generics
    Replies: 2
    Last Post: November 9th, 2011, 07:03 PM
  5. Generating random numbers
    By abelned in forum Object Oriented Programming
    Replies: 1
    Last Post: September 1st, 2010, 07:24 AM

Tags for this Thread