Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How print all combinatinos of 000-999 with one loop?

800670Jan 18 2011 — edited Jan 18 2011
Hi!

I would like to print all combinations of n integers.
For instance, the below code prints all combinations of integers 000-999:
public class Test {    
    public Test(){
        for(int a = 0; a < 10; a++){
            for(int b = 0; b < 10; b++){
                for(int c = 0; c < 10; c++){
                    System.out.println((a+""+b+""+c));
                }
            }
        }
    }

    public static void main(String[] args){
        new Test();
    }
}
If I want all combinations between 00000-99999 I can add two more for-loops. But is it possible to make the code nicer by, for example, create a recursive method or something simillar? I tried but failed since the recursive loop always ended up in itself and throwed a java.lang.StackOverflowError.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 15 2011
Added on Jan 18 2011
14 comments
2,817 views