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 8 of 8

Thread: Need some help with grouping an arraylist

  1. #1
    Junior Member
    Join Date
    Jan 2018
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need some help with grouping an arraylist

    I need to make every possible version of a list of groups by a text file full of names.

    I have the part where it makes unique groups (it all gets stored in a List<List<String>> called rootList)
    And that holds this:
    [a, b]
    [a, c]
    [a, d]
    [b, c]
    [b, d]
    [c, d]

    And now I want to make another List<List<String>> called resultList that saves the information from rootList as:
    List 1:
    [a, b]
    [c, d]

    List 2:
    [a, c]
    [b, d]

    List 3:
    [a, d]
    [b, c]

    How would I go about doing this?

    Source:
    package com.company;
     
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.regex.Pattern;
     
    public class createGroups
    {
        public static List<List<String>> rootList = new ArrayList<List<String>>();
        public static List<List<String>> resultList = new ArrayList<List<String>>();
        public static String comb = "";
     
        public static void main(String[] args) {
            String[] names = new String[] {"Rik", "Erwin", "Ian", "Mustafa"};
     
     
     
            int r = 2;
            int n = names.length;
            String[] data = new String[r];
            String[] groups = createGroups.combinationUtil(names, data, 0, n-1, 0, r);
        }
     
        static void printCombination(String arr[], int n, int r)
        {
            // A temporary array to store all combination one by one
            String[] data = new String[r];
     
            // Print all combination using temprary array 'data[]'
            combinationUtil(arr, data, 0, n-1, 0, r);
        }
     
        /* arr[]  ---> Input Array
           data[] ---> Temporary array to store current combination
           start & end ---> Staring and Ending indexes in arr[]
           index  ---> Current index in data[]
           r ---> Size of a combination to be printed */
        static String[] combinationUtil(String[] arr, String[] data, int start, int end, int index, int r)
        {
            if (index == r)
            {
     
                for (int j=0; j<r; j++)
                {
                    if (j == 1)
                    {
                        comb += data[j];
                    }else if(j == 0){
                        comb += data[j] + ", ";
                    }
     
                }
     
                List<String> node = new ArrayList<String>();
                node.add(comb);
                rootList.add(node);
                comb = "";
     
                return data;
            }
            for (int i=start; i<=end && end-i+1 >= r-index; i++)
            {
                data[index] = arr[i];
                combinationUtil(arr, data, i+1, end, index+1, r);
            }
     
     
            if(start == 0){
                for (List<String> str: rootList){
                    System.out.println(str);
                }
                System.out.println("Done");
            }
            return data;
        }
    }

    The output right now is:
    [Rik, Erwin]
    [Rik, Ian]
    [Rik, Mustafa]
    [Erwin, Ian]
    [Erwin, Mustafa]
    [Ian, Mustafa]
    Done

    But what I want is:
    List 1:
    [Rik, Erwin]
    [Ian, Mustafa]

    List 2:
    [Rik, Ian]
    [Erwin, Mustafa]

    List 3:
    [Rik, Mustafa]
    [Erwin, Ian]
    Last edited by airgmbop; January 26th, 2018 at 08:08 AM. Reason: Added source

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need some help with grouping an arraylist

    How do you execute the program for testing? I do not see a main() method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2018
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need some help with grouping an arraylist

    Quote Originally Posted by Norm View Post
    How do you execute the program for testing? I do not see a main() method.
    Oh sorry I forgot, it gets called from a JPanel clickhandler.
    A file gets converted to an array and eventually it runs this:
    createGroups.combinationUtil(names, data, 0, n-1, 0, r);
    In which "names" is the array that holds all the names and data looks like this: String[] data = new String[r];
    And r is the amount of people in one group, in this case 2 and n is the length of the names array
    All the other parameters can be found in the constructor (combinationUtil)
    Last edited by airgmbop; January 25th, 2018 at 12:11 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need some help with grouping an arraylist

    Do you have a small program with a main that could be used for testing your code?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2018
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need some help with grouping an arraylist

    Quote Originally Posted by Norm View Post
    Do you have a small program with a main that could be used for testing your code?
    Will poste one when I get home

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need some help with grouping an arraylist

    Be sure to post the program's output with some comments saying what is wrong and show the desired output.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jan 2018
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need some help with grouping an arraylist

    Quote Originally Posted by Norm View Post
    Be sure to post the program's output with some comments saying what is wrong and show the desired output.
    Sorry for keepin you wait so ling. I edited the code with a main.
    It also has the current output and the desired output.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need some help with grouping an arraylist

    Ok, now where is the documentation/ design specifications on how the code is going to solve the problem?
    Do you have an algorithm? Please post the steps in the algorithm that the program needs to take to solve the problem.

    You need to make a design before writing any code.

    Note: For ease of debugging I suggest using digits vs names:
           String[] names = new String[]   {"1","2", "3", "4"};
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. grouping jcheck button's
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 27th, 2012, 03:25 AM
  2. Challenging Java Question: Test your Java skill by grouping these terms
    By karthickk3 in forum Java Theory & Questions
    Replies: 3
    Last Post: July 18th, 2012, 10:10 PM
  3. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  4. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  5. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM