title: Iterating through a character array in Java - improving algorithm
body: <p>I'm trying to iterate through my array to produce all possible combinations of the given char array.</p>
  
  <p>If the length I specify is 4 then I want it to iterate through all combinations of the chars in the array up to a length of 4. </p>
  
  <p>It would look something like this:</p>
  
  <pre><code>char[] charArray = "abcdefghijklmnopqrstuvwxyz".toCharArray();
  </code></pre>
  
  <p>Output of method I want:</p>
  
  <p>a,
  b,
  c,
  ...,
  x,
  y,
  z,
  aa,
  ab,
  ac,
  ...,
  ax,
  ay,
  az,
  ba,
  bb,
  bc,
  ...,
  bx,
  by,
  bz,
  ca,
  cb,
  cc,
  ...
  zzzx,
  zzzy,
  zzzz</p>
  
  <p>Here's some code:</p>
  
  <pre><code>cs = charArray;
  cg = new char[4]; // 4 up to 4 characters to guess
  
  int indexOfCharset = 0; // should I be using all these?
  int indexOfCurrentGuess = 0;
  int positionInString = 0;
  
  public void incrementNew() {
      // 1 DIGIT guesses
      if (cg.length == 0) {
          if (indexOfCharset == cs.length) {
              cg = new char[cg.length + 1];
          } else {
              cg[positionInString] = nextChar();
          }
      }
      // 2 DIGIT guesses
      else if (cg.length == 1) {
          if (cg[0] == cs.length &amp;&amp; cg[1] == cs.length) {
              cg = new char[cg.length + 1];
          } else {
              ... Something goes here &lt;-
              cg[positionInString] = nextChar();
          }
      }
      System.out.println("cg[0]=" + cg[0]);
  }
  
  public char nextChar() {
      char nextChar;
      if (indexOfCharset &lt; cs.length) {
          nextChar = cs[indexOfCharset];
      } else {
          indexOfCharset = 0;
          nextChar = cs[indexOfCharset];
      }
      indexOfCharset++;
      //System.out.println("nextChar = " + nextChar);
      return nextChar;
  
  }
  </code></pre>
  
  <p>The only way I can think of doing it is using lots of IF statements - is there an algorithm or way to do it neater? If not then any suggestions on how to deal with two or more characters?</p>
  
  <p>EDIT:</p>
  
  <p>I want it to work for any unsorted char arrays not just a-z.</p>
  
  <p>All the implementations I've found only work for sorted arrays..</p>