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: hi i need help with the outputs for number combinations, I want to restrict the outputs to suits of 3 numbers

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default hi i need help with the outputs for number combinations, I want to restrict the outputs to suits of 3 numbers

    Hi i need help to refine the code below, I want to be able to
    (A) Restrict the number of output values per line to 3 numbers,
    (B) Remove Duplicate lines, where the same numbers are duplicated just outputted in a different order.
    (C) Count the number of outlines
    (D) Add a fixed column to the outputs that has an ascending count from 1 upwards

    thanks a lot i'm very new to Coding and was able to come across this which aided in the concept of what i'm trying to achieve
    the code is as follows:

    package num.com.t1;
    import java.util.*;
    import java.util.concurrent.atomic.AtomicInteger;
    /**
    *
    *
    */
    public class NumComT1 {


    public static void main(String... args) {
    Bag<Integer> b = new Bag<>();
    b.countFor(1, 1);
    b.countFor(2, 1);
    b.countFor(3, 1);
    b.countFor(4, 1);
    b.countFor(5, 1);
    b.countFor(6, 1);
    b.countFor(7, 1);
    b.countFor(8, 1);

    Set<String> set = new LinkedHashSet<>();
    for (List<Integer> list : b.combinations()) {
    System.out.println(list);
    String s = list.toString();
    if (!set.add(s))
    System.err.println("Duplicate entry " + s);
    }
    }
    }

    class Bag<E> {
    final Map<E, AtomicInteger> countMap = new LinkedHashMap<>();

    void countFor(E e, int n) {
    countMap.put(e, new AtomicInteger(n));
    }

    void decrement(E e) {
    AtomicInteger ai = countMap.get(e);
    if (ai.decrementAndGet() < 1)
    countMap.remove(e);
    }

    void increment(E e) {
    AtomicInteger ai = countMap.get(e);
    if (ai == null)
    countMap.put(e, new AtomicInteger(1));
    else
    ai.incrementAndGet();
    }

    List<List<E>> combinations() {
    List<List<E>> ret = new ArrayList<>();
    List<E> current = new ArrayList<>();
    combinations0(ret, current);
    return ret;
    }

    private void combinations0(List<List<E>> ret, List<E> current) {
    if (countMap.isEmpty()) {
    ret.add(new ArrayList<E>(current));
    return;
    }
    int position = current.size();
    current.add(null);
    List<E> es = new ArrayList<>(countMap.keySet());
    if (es.get(0) instanceof Comparable)
    Collections.sort((List) es);
    for (E e : es) {
    current.set(position, e);
    decrement(e);
    combinations0(ret, current);
    increment(e);
    }
    current.remove(position);
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: hi i need help with the outputs for number combinations, I want to restrict the outputs to suits of 3 numbers

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags which are explained near the top of the above link.

Similar Threads

  1. Not getting the right outputs.
    By DarkestLord in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 15th, 2014, 03:42 PM
  2. MuliThreading Unable to get outputs but many NPES.....
    By tanchiwoo in forum What's Wrong With My Code?
    Replies: 12
    Last Post: February 17th, 2014, 01:18 PM
  3. Why do I get 0 in the outputs?
    By jean28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 30th, 2012, 12:00 PM
  4. Code only outputs 1's
    By LoganC in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 27th, 2012, 09:26 PM
  5. LinkedList outputs ONLY last element
    By hexwind in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 30th, 2011, 04:57 AM