生成固定位数的字母流水号?
生成固定位数的字母流水号?
------解决方案--------------------public class Test {
public static void main(String[] args) {
IdentifierAlpha id = new IdentifierAlpha(4, 'A ', 'Z ');
for (int i = 0; i < 17575; i++) {
id.next();
}
// 应该输出 AZZZ
System.out.println(id.next());
// 应该输出 BAAA
System.out.println(id.next());
}
}
class IdentifierAlpha {
private char[] sequence;
private char start;
private char end;
private IdentifierAlpha() {
}
public IdentifierAlpha(int bit, char start, char end) {
if(start > end) {
char tmp = end;
end = start;
start = tmp;
}
this.start = start;
this.end = end;
sequence = new char[bit + 1];
sequence[sequence.length - 1] = (char) (start - 1);
for (int i = sequence.length - 2; i > 0; i--) {
sequence[i] = start;
}
sequence[0] = '_ ';
}
/**
* 每次增加一个字母,并且循环使用,如 ZZZZ,下一个则为 AAAA
* @return
*/
public String next() {
if (sequence == null) {
return null;
}
sequence[sequence.length - 1] += 1;
for (int i = sequence.length - 1; i > 0; i--) {
if (sequence[i] > end) {
sequence[i] = start;
sequence[i - 1] = (i != 1) ? (char)(sequence[i - 1] + 1) : '_ ';
} else {
break;
}
}
return new String(sequence, 1, sequence.length - 1);
}
}
------解决方案--------------------帮顶,接分