title: Is there a way to iterate through an array without using loops?
body: <p>I'm trying to write a code that will output all possible passwords from a given array recursively,
  e.g. given the input</p>
  <pre><code>&quot;ab&quot;
  </code></pre>
  <p>will output the next:</p>
  <pre><code>a 
  b 
  aa
  ab
  ba
  bb 
  </code></pre>
  <p>the problem is that I'm instructed to use only one loop in the <code>crack()</code> function below and that's it. I cannot use any other functions, just those two.</p>
  <p>This is what I've got so far:</p>
  <pre><code>import java.util.*;
  
  public class PasswordGen {
  
     public static void main(String[] args) {
      
       Scanner sc = new Scanner(System.in);
       System.out.println(&quot;Please enter a string:&quot;);
       char array[] = sc.next().toCharArray();
       System.out.println(&quot;All Combination:&quot;);
  
       crack(array);
      
       sc.close();
     }
  
     static void crack(char[] array) {        
       for (int i = 1; i &lt;= array.length; i++) {
          generate(array, i, &quot;&quot;, array.length);
       }
     }
  
     static void generate(char[] array, int i, String string, int length) {
      
       //recursion stopping if condition is meet 
       if (i == 0) {
          System.out.println(string);
          return;
       }
      
       for (int j = 0; j &lt; length; j++) {         
          String charArray = string + array[j];
          generate(array, i -1, charArray , length);
       }
      
       return;
     }
  }
  </code></pre>
  <p>Is there a way to get the same output without using the <code>for</code> loop in the <code>generate()</code> function?
  (see code below).</p>
  <pre><code>    static void generate(char[] array, int i, String string, int length) {
      
      //recursion stopping if condition is meet 
      if (i == 0) {
          System.out.println(string);
          return;
      }
      
      for (int j = 0; j &lt; length; j++) {
          
          String charArray = string + array[j];
          generate(array, i -1, charArray , length);
      }
  </code></pre>
  <p>Any suggestions/better way to do it?</p>