How to make infinite character combinations

Question from Bulver#9256

Hey, please forgive my bad english. I am trying to get an infinite amount of char combinations which are stored in an array. I cant figure out how i can implement this in one method. Does anyone have ideas?

public static void one(char[] arr){
       for(int i = 0; i<95; i++){
           String a = ""+arr[i];
           if(hash2(a)%m==0){
               System.out.println(a);
           }
       }
   }

   public static void two(char[] arr){
       for(int i = 0; i<95; i++){
           for(int j = 0;j<95;j++){
               String a = ""+arr[i]+arr[j];
               if(hash2(a)%m==0){
                   System.out.println(a);
               }
           }
       }
   }

any ideas on how to infinitely stack for loops?

I don't know how many digits i need

I'd like to have one method which runs till i stop it and checks every combination

Do you understand my problem?

public static void three(char[] arr){
        for(int i = 0; i<95; i++){
            for(int j = 0;j<95;j++){
                for(int k = 0; k<95;k++){
                    String a = ""+arr[i]+arr[j]+arr[k];
                    if(hash2(a)%m==0){
                       System.out.println(a);
                    }
                }
            }
        }
    }

There must be a better way to check for more digits

One way - Instead of having all the digits be "on the stack"

String a = ""+arr[i]+arr[j]+arr[k];

store each loop's state in an object

public final class CharIterator implements Iterator<String> {
    private int i;

    public boolean hasNext() {
        return i < 95;
    }

    public String next() {
        char c = (char) i;
        i++;
        return Character.toString(c);
    }
}

and then all you need is a way to

  1. make an Iterable from that (should be fairly simple)
  2. chain two iterables into one larger iterable

basically make this

public final class IterableChain<T> implements Iterable<T> {
    public <A, B> IterableChain(Iterable<A> i1, Iterable<B> i2, Function<A, B, T> combine) { }

    // ...
}

and then all you'll need to do is

Iterable<String> iterable = new CharIterable();
for (int i = 0; i < 10; i++) {
    iterable = new IterableChain(new CharIterable(), iterable);
}

for (String s : iterable) {
    ...
}

does that make sense?

Yes, thanks alot!!


<- Index