This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to make combination function generic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to make combination function generic " Watch "How to make combination function generic " New topic

How to make combination function generic

Anjali Pal

Joined: Oct 03, 2009
Posts: 16
Hi all,

I have written a function which generates combination in a desired order. The code is somthing like this

The output of this code is and this is what is required :

My problem is all the hard coding in that function i am passing "min" "max" for range of digits and this part is ok (more or less)
Please ignore the Boolean value.

"noOfControls" is something i need to implement ie all the combination i am getting are 3 digits max (governed by number of for loop).

I need to remove this dependency i don't want to write new method every time say "if i need series upto 6 digits and write method with 6 loops"

My question is what should be the approach ?
and what are the possible ways to do it.

Thanks in advance.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20183

Check out recursion. You create a private method that takes the number of elements to add. When initially called with 6, you take one element, then call the same method but with 5 this time. Continue until the count is 1.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: How to make combination function generic
It's not a secret anymore!