code: private void generateNextPermutationIndices()
      {
          if (remainingPermutations == 0)
          {
              throw new IllegalStateException("There are no permutations remaining.  " +
                                              "Generator must be reset to continue using.");
          }
          else if (remainingPermutations < totalPermutations)
          {
              // Find largest index j with permutationIndices[j] < permutationIndices[j + 1]
              int j = permutationIndices.length - 2;
              while (permutationIndices[j] > permutationIndices[j + 1])
              {
                  j--;
              }
  
              // Find index k such that permutationIndices[k] is smallest integer greater than
              // permutationIndices[j] to the right of permutationIndices[j].
              int k = permutationIndices.length - 1;
              while (permutationIndices[j] > permutationIndices[k])
              {
                  k--;
              }
  
              // Interchange permutation indices.
              int temp = permutationIndices[k];
              permutationIndices[k] = permutationIndices[j];
              permutationIndices[j] = temp;
  
              // Put tail end of permutation after jth position in increasing order.
              int r = permutationIndices.length - 1;
              int s = j + 1;
  
              while (r > s)
              {
                  temp = permutationIndices[s];
                  permutationIndices[s] = permutationIndices[r];
                  permutationIndices[r] = temp;
                  r--;
                  s++;
              }
          }
          --remainingPermutations;
      }
docs: Generate the indices into the elements array for the next permutation.  The
  algorithm is from Kenneth H. Rosen, Discrete Mathematics and its Applications,
  2nd edition (NY: McGraw-Hill, 1991), p. 284)