0

I want to extract all possible combination from the following
the array list that holds all the data has this structure

ArrayList<object> students = new ArrayList<object>();
ArrayList<object> staff = new ArrayList<object>();
ArrayList<object> uni = new ArrayList<object>();
ArrayList<object> exam = new ArrayList<object>();

ArrayList<ArrayList<object>> main = new ArrayList<object>();
main.add(this.students);
main.add(this.staff);
main.add(this.uni);
main.add(this.exam);

class object has but two variables

class object {
    public String name; 
    public double cost;
}

this main variable hold the following data as indicated in the table and each column might have from 1 to 100 value but that doesn't mean that each column must have the same number of values

| students | staff | uni---| exam |
|500--------|400--|1400 |20000 |
|350--------|250--|1100 |10000 |

So what I want to extract is something like that
500=>400=>1400=>20000
500=>400=>1400=>10000

500=>400=>1100=>20000
500=>400=>1100=>10000

500=>250=>1400=>20000
500=>250=>1400=>10000

500=>250=>1100=>20000
500=>250=>1100=>10000

350=>400=>1400=>20000
350=>400=>1400=>10000

350=>400=>1100=>20000
350=>400=>1100=>10000

350=>250=>1400=>20000
350=>250=>1400=>10000

350=>250=>1100=>20000
350=>250=>1100=>10000

how can I do that?

4
  • JavaScript? ;D! Commented Mar 18, 2017 at 17:24
  • my mistake XD!! Commented Mar 18, 2017 at 17:26
  • 2
    class object { Please name your classes appropriately. This is unacceptable in the Java world. Commented Mar 18, 2017 at 18:35
  • i just name it this way because I'm trying to achieve something here and I don't need any naming convention because this is just a test of my code Commented Mar 18, 2017 at 19:27

1 Answer 1

2

How about a simple recursive function like this ?

ArrayList<ArrayList<object>> combs = new ArrayList<>();
object[] comb = new object[main.size()];

void generate(int pos) {
    if (pos == comb.length)
        combs.add(new ArrayList<object>(Arrays.asList(comb)));
    else {
        ArrayList<object> arr = main.get(pos);
        for (object el : arr) {
            comb[pos] = el;
            generate(pos + 1);
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.