[英]Generate one to five character length string consisting of a-z and 0-9
I'd like to generate all possible strings, from length 1 to length 5, consisting of the characters az and 0-9. 我想生成所有可能的字符串,从长度1到长度5,由字符az和0-9组成。
I've found snippets of code that can probably help me, but I am stuck on modifying them to achieve what I need. 我发现了可能可以帮助我的代码片段,但是我一直坚持修改它们以实现所需。
This bit of code I found on SO will produce all possible string combinations of length 4 using az and 0-9, 1679616 strings. 我在SO上找到的这段代码将使用az和0-9、1679616字符串生成长度为4的所有可能字符串组合。
public static void generate(){
int MAX = 36;
long count = 1L * MAX * MAX * MAX * MAX;
int counter = 0;
char[] alphabet = "0123456789abcdefghijklmnopqrstuvwxyz".toCharArray();
int[] digits = new int[4];
char[] output = "0000".toCharArray();
for (int i= 0; i < count; i++){
String name = String.valueOf(output);
for(int d = 3; d >=0; --d){
digits[d] = (digits[d] + 1) % MAX;
output[d] = alphabet[digits[d]];
if (digits[d] > 0){
break;
}
}
counter++;
System.out.println(name);
}
System.out.println(counter);
}
Now that's not what I need exactly, but it's close and maybe I can make it work! 现在,这不是我真正需要的,但是已经很接近了,也许我可以使它工作! I've tried modifying in it the following way but there are index issues that are throwing ArrayOutBounds exceptions everywhere.
我曾尝试通过以下方式对其进行修改,但是索引问题到处都引发ArrayOutBounds异常。 The thought process is, I can just wrap this entire block in another for loop that deals with the size of digits and output, which would go from 1 to 5. The more time I spend modifying, the uglier it gets and the more I am thinking, "there must be a cleaner way".
考虑的过程是,我可以将整个块包装在另一个for循环中,该循环处理数字和输出的大小,从1到5。我花的修改时间越多,获取的丑陋程度就越大。思考,“必须有一种更清洁的方式”。
public static void generate(){
int MAX = 36;
long count = 1L * MAX * MAX * MAX * MAX * MAX;
String build = null;
int counter = 0;
char[] alphabet = "0123456789abcdefghijklmnopqrstuvwxyz".toCharArray();
int[] digits = null;
char[] output = null;
for (int j = 1; j < 5; j++){
build = null; //clear build
digits = null; //clear digits
outputs = null; //clear outputs
digits = new int[j]; //initialize digits to size j
//small loop to append "0" to build
for (int m = 0; m < j; m++){
build = build + "0";
}
output = build.toCharArray(); //convert our string to charArray
for (int i= 0; i < count; i++){
String name = String.valueOf(output);
for(int d = 3; d >=0; --d){
digits[d] = (digits[d] + 1) % MAX;
output[d] = alphabet[digits[d]];
if (digits[d] > 0){
break;
}
}
counter++;
System.out.println(name);
}
}
System.out.println(counter);
}
You need to modify the lines 您需要修改行
long count = 1L * MAX * MAX * MAX * MAX * MAX; // one more * MAX
...
int[] digits = new int[5]; // from [4]
char[] output = "00000".toCharArray(); // from 0000
...
for(int d = 4; d >=0; --d){ // from d = 3
More generally, if n
is the number of digits, 更一般而言,如果
n
是位数,
long count = 1L;
for( int i=0; i<n; i++ ) count *= MAX;
...
int[] digits = new int[n];
char[] output = new char[n];
Arrays.fill( output, '0' );
...
for(int d = n-1; d >=0; --d){
You could modify generate
to take a leading character as a parameter and print all 5-character strings that start with that character. 您可以修改
generate
以将前导字符作为参数,并打印以该字符开头的所有5个字符的字符串。 Then call this modified generate
with each of the possible leading characters. 然后使用每个可能的前导字符调用此修改后的
generate
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.