生成固定位数的字母流水号?

解决方案 »

  1.   

    类似这样的
    AAAA
    AAAB
    AAAC
    ....
      

  2.   

    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);
      }
    }
      

  3.   

    写一下我自己写的,对比一下上面的,自己的好像比较有局限性。
    谢谢:bao110908(长牙了,好痛) char   changeChar ='A';
    int charint = 0;
    String nowFeeCode = "ZEYJDO";
      char tempChar = '0';
      charint =   (int)changeChar; 
      
     for(int count =0;count < nowFeeCode.length(); count++){
      changeChar = nowFeeCode.charAt(count);
      charint =   (int)changeChar; 
      
      if (charint<=89){
      tempChar =  (char)(charint+1);
      nowFeeCode = nowFeeCode.substring(0,count) + tempChar + nowFeeCode.substring(count+1);
      tempChar = '0';
     
      }
      else{
      tempChar = 'A';
       nowFeeCode = nowFeeCode.substring(0,count) + tempChar + nowFeeCode.substring(count+1);
      
      }
      if (tempChar == '0'){
      break;
      }
      
      }
      System.out.println("Loop nowFeeCode = " + nowFeeCode);