File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes All combinations of an int array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "All combinations of an int array" Watch "All combinations of an int array" New topic
Author

All combinations of an int array

S Ali
Ranch Hand

Joined: Aug 23, 2009
Posts: 129
Hi all,
I need to make a recursive function that can make me all possible combinations of an int array, I don't want to use a for loop because the size of the array is user input. Could anybody please guide me thought this because I have no idea how to implement a recursive function.
ex: int array[] {1,2,3,4}
possibilities :
0
1
2
3
4
12
13
14
123
1234
134
23
234
34
24
124

SCJP 6
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Do you know what a recursive function is? It's is a function that calls itself, basically. If you know how to write method in java, then having a method call itself is pretty easy. You just have to work out when it needs to stop calling itself so you don't end up in an infinite loop. The best thing for you do to is try something and then post that code here and we can help you figure it out. No one is going to write it for you.


GenRocket - Experts at Building Test Data
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Don't try to reinvent the wheel. Read this first:
http://www.cs.utsa.edu/~wagner/knuth/
The Art of Computer Programming Volume 4, Combinatorial Algorithms
chapter 7.2.1.3. Generating all combinations
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I disagree: *do* try to reinvent the wheel when you're learning something--otherwise you're just regurgitating somebody else's intelligence.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
You might benefit from having a look at the recursive solution to the factorial problem. The factorial problem does have one or two things in common with your array problem.

Here is where I got my feet wet with recursion:

http://erwnerve.tripod.com/prog/recursion/recintro.htm
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18829
    
  40


When it comes to recursion, I am definitely *not* a fan of learning by example.

If you look at a solution, in my opinion, at best, you just learn how that solution works. And if you are unlucky, you only think you know how it works.

Recursion requires a way of thinking out the problem. And if you never try to figure it out, with the easy examples, how can you do the hard ones? You need to mentally exercise your recursion muscles, on the easy problems, to be strong enough for the hard ones.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Henry Wong wrote:
When it comes to recursion, I am definitely *not* a fan of learning by example.

If you look at a solution, in my opinion, at best, you just learn how that solution works. And if you are unlucky, you only think you know how it works.

Recursion requires a way of thinking out the problem. And if you never try to figure it out, with the easy examples, how can you do the hard ones? You need to mentally exercise your recursion muscles, on the easy problems, to be strong enough for the hard ones.

Henry


I disagree, but you're the mod, so my opinion must be wrong. sigh. I knew there was a reason why I quit this board. Too many people who insist that their way is the best.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18829
    
  40

Fred Hamilton wrote:
I disagree, but you're the mod, so my opinion must be wrong. sigh. I knew there was a reason why I quit this board. Too many people who insist that their way is the best.


How does not being a fan of something impose on you? Or are you saying that you disagree, and that I am a fan?

Or are you saying that since I am a mod, I shouldn't give my opinion, because it means that everyone else is wrong?

Henry
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Fred Hamilton wrote:I disagree, but you're the mod, so my opinion must be wrong. sigh. I knew there was a reason why I quit this board. Too many people who insist that their way is the best.

Um... he even *said* "in my opinion". I don't get your reaction at all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: All combinations of an int array