Java 递增字符串/字符数组

Java 递增字符串/字符数组,java,loops,iteration,Java,Loops,Iteration,编辑注:我不是很清楚。我试着从一个字符的猜测开始,比如0到00到000。。。一直到zzzz。基本上,从0到zzzz的所有可能迭代。对不起,我不是很清楚 我目前正在尝试循环遍历一组字符。数组包含0-9和a-z(小写)。诚然,这是一个家庭作业——我是一个无用的程序员(见上一篇文章),我需要一些帮助 我想要的是遍历char数组的所有可能结果并列出结果 aaa aba aab > through to > aca aac ada

编辑注:我不是很清楚。我试着从一个字符的猜测开始,比如0到00到000。。。一直到zzzz。基本上,从0到zzzz的所有可能迭代。对不起,我不是很清楚

我目前正在尝试循环遍历一组字符。数组包含0-9和a-z(小写)。诚然,这是一个家庭作业——我是一个无用的程序员(见上一篇文章),我需要一些帮助

我想要的是遍历char数组的所有可能结果并列出结果

aaa                aba
aab > through to > aca
aac                ada
如果它只是基于字母,我读过,我可以基于base26数字系统,但这包括数字

到目前为止,我已经成功地在数组中循环,将答案分配给“猜测”数组,然后在下一个位置循环。在那之后,我被难住了

如上次所示,任何建议都将不胜感激。这项工作是基于暴力的,但如果我的真正目的是非法的,我可以使用大量的工作实例,但事实并非如此

这是我到目前为止所拥有的

/**
 *
 * @author Aaron
 */
public class Test {

    /**
     * @param args the command line arguments
     */
    int current = 0;
    char[] guess = new char[6];

    public static void main(String[] args) {
        Test test = new Test();
        int maxLength = 6; 
        char c = '0';

        while (maxLength != 0) {
            maxLength--;
            test.iterateAll(c);
            test.increment(c);            
        }
    }

    public void iterateAll(char c) {
        char[] charset = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                          'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                          'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
                          'u', 'v', 'w', 'x', 'y', 'z'};
        for (int i = 0; i < charset.length; i++) {
            //c = charset[i];
            guess[current] = charset[i];
            System.out.println(guess);
        }
    }

    public void increment(char c) {
        current++;
    }
}
/**
*
*@作者亚伦
*/
公开课考试{
/**
*@param指定命令行参数
*/
int电流=0;
字符[]猜测=新字符[6];
公共静态void main(字符串[]args){
测试=新测试();
int maxLength=6;
字符c='0';
while(最大长度!=0){
最大长度--;
试验。迭代法(c);
试验.增量(c);
}
}
公共无效迭代尔(字符c){
char[]charset={'0','1','2','3','4','5','6','7','8','9',,
‘a’、‘b’、‘c’、‘d’、‘e’、‘f’、‘g’、‘h’、‘i’、‘j’,
‘k’、‘l’、‘m’、‘n’、‘o’、‘p’、‘q’、‘r’、‘s’、‘t’,
‘u’、‘v’、‘w’、‘x’、‘y’、‘z’};
for(int i=0;i
你能用吗?如果是这样,它愿意为你做大部分工作。下面打印所有内容的aaa、aab、aac等

final int start = 36*36*10 + (36*10) + 10;
for (int i = start; i < 36*36*36; ++i) {
    final String base36 = Integer.toString(i, 36);
    final String padded = String.format("%3s", base36).replace(' ', '0');
    System.out.println(padded);
}
final int start=36*36*10+(36*10)+10;
对于(int i=开始;i<36*36*36;++i){
最终字符串base36=整数.toString(i,36);
填充的最终字符串=String.format(“%3s”,base36)。替换(“”,'0');
系统输出打印项次(填充);
}

我会使用StringBuilder,让您在字符级别“操作”字符串。这里它只是向上,在pos“寄存器”中从左到右携带值,这些只是索引字符序列。另外请注意,字符只是数字的一种类型,因此可以用作循环中的文字

char[] seq = new char[36];
int i = 0;
for (char c = '0'; c <= '9'; c++) {
    seq[i++] = c;
}
for (char c = 'a'; c <= 'z'; c++) {
    seq[i++] = c;
}

int length = 3;
StringBuilder builder = new StringBuilder("    ");

int[] pos = new int[length];
int total = (int) Math.pow(seq.length, length);
for (int count = 0; count < total; count++) {
    for (int x = 0; x < length; x++) {
        if (pos[x] == seq.length) {
            pos[x] = 0;
            if (x + 1 < length) {
                pos[x + 1]++;
            }
        }
        builder.setCharAt(x, seq[pos[x]]);
    }
    pos[0]++;

    System.out.println(builder.toString());
}
char[]seq=新字符[36];
int i=0;

对于(char c='0';c你使用基数26的想法很好,事实上你也有数字,这意味着你应该选择基数36。FauxFaux的答案似乎使用了这个事实。我没有考虑基数36,但这是一个很好的答案!你能解释一下这个例子的数学吗?为什么开始变量是129970?为什么我们需要36*36*36?我希望能够增加到并包括六个字符,那么我会将其更改为36*36*36*36*36*36*36*36*36*36吗?谢谢!起始数字是aaa,
10
表示
a
,而
36
是字母表的大小;要获得一系列
3
s,
333
,在base-
10
中,我们应该做(10*10*3+10*3+3)?