JAVA自定義進制類

項目上的需求,需要把一個指定的列號(整型)換算爲Excel的字符列號(類似於AA、AB等),於是寫了下面這個類,可支持任意自定義進制,比如3進制,4進制等等諸如此類,並支持設置起始最小值,起始Excel的列號就是最小值爲1的27進制,代碼如下:

 

  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.  
  4. /**  
  5.  * 自定義進制類  
  6.  *   
  7.  * @category 框架工具實體類  
  8.  * @author 王鍾沛  
  9.  * @version 1.0  
  10.  */ 
  11. public class CustomDigit {  
  12.  
  13.     /** 十進制數 */ 
  14.     private Integer decimal;  
  15.  
  16.     /** 用於儲存轉換後數據的列表 */ 
  17.     private List<CustomInteger> numberList = new ArrayList<CustomInteger>();  
  18.  
  19.     /** 位數,比如2、8、10、16、60等等任意正整數 */ 
  20.     private Integer digit;  
  21.  
  22.     /** 最小值,默認爲0 */ 
  23.     private Integer minValue = 0;  
  24.  
  25.     public CustomDigit() {  
  26.         super();  
  27.     }  
  28.  
  29.     public CustomDigit(Integer decimal, Integer digit) {  
  30.         this.decimal = decimal;  
  31.         this.digit = digit;  
  32.         numberList.add(new CustomInteger(0));  
  33.     }  
  34.  
  35.     public CustomDigit(Integer decimal, Integer digit, Integer minValue) {  
  36.         this.decimal = decimal;  
  37.         this.digit = digit;  
  38.         this.minValue = minValue;  
  39.         numberList.add(new CustomInteger(0));  
  40.     }  
  41.  
  42.     /**  
  43.      * @return the decimal  
  44.      */ 
  45.     public Integer getDecimal() {  
  46.         return decimal;  
  47.     }  
  48.  
  49.     /**  
  50.      * @param decimal  
  51.      *            the decimal to set  
  52.      */ 
  53.     public void setDecimal(Integer decimal) {  
  54.         this.decimal = decimal;  
  55.     }  
  56.  
  57.     /**  
  58.      * @return the numberList  
  59.      */ 
  60.     public List<CustomInteger> getNumberList() {  
  61.         return numberList;  
  62.     }  
  63.  
  64.     /**  
  65.      * @param numberList  
  66.      *            the numberList to set  
  67.      */ 
  68.     public void setNumberList(List<CustomInteger> numberList) {  
  69.         this.numberList = numberList;  
  70.     }  
  71.  
  72.     /**  
  73.      * @return the digit  
  74.      */ 
  75.     public Integer getDigit() {  
  76.         return digit;  
  77.     }  
  78.  
  79.     /**  
  80.      * @param digit  
  81.      *            the digit to set  
  82.      */ 
  83.     public void setDigit(Integer digit) {  
  84.         this.digit = digit;  
  85.     }  
  86.  
  87.     /**  
  88.      * @return the minValue  
  89.      */ 
  90.     public Integer getMinValue() {  
  91.         return minValue;  
  92.     }  
  93.  
  94.     /**  
  95.      * @param minValue  
  96.      *            the minValue to set  
  97.      */ 
  98.     public void setMinValue(Integer minValue) {  
  99.         this.minValue = minValue;  
  100.     }  
  101.  
  102.     public void clear() {  
  103.         this.decimal = Integer.valueOf(0);  
  104.         this.digit = Integer.valueOf(0);  
  105.         this.minValue = Integer.valueOf(0);  
  106.         this.numberList.clear();  
  107.     }  
  108.  
  109.     public Integer getLength() {  
  110.         return this.numberList.size();  
  111.     }  
  112.  
  113.     private List<CustomInteger> formattedNumberList() {  
  114.         int initialNumber = decimal.intValue();  
  115.         while (initialNumber > 0) {  
  116.             int tempNumber = initialNumber > digit.intValue() ? digit  
  117.                     .intValue() : initialNumber;  
  118.             CustomInteger lastNumber = this.numberList.get(getLength() - 1);  
  119.             lastNumber.setValue(lastNumber.getValue() + tempNumber);  
  120.             initialNumber = initialNumber - tempNumber;  
  121.             arrangementList(getLength() - 1);  
  122.         }  
  123.           
  124.         return this.numberList;  
  125.     }  
  126.  
  127.     private void arrangementList(Integer index) {  
  128.         CustomInteger lastNumber = this.numberList.get(index);  
  129.         if (lastNumber.getValue() >= digit) {    
  130.             boolean flag = false;  
  131.             int value = lastNumber.getValue() - digit + minValue;   
  132.             if(value >= digit){  
  133.                 flag = true;  
  134.                 // 防止+minValue之後又再次大於進制的閥值  
  135.                 value = value - digit + minValue;   
  136.             }  
  137.             // 如果再次大於閥值,則向上一位進2,否則進1  
  138.             int count = flag ? 2 : 1;  
  139.             lastNumber.setValue(value);  
  140.             if (index.intValue() == 0) {  
  141.                 this.numberList.add(0new CustomInteger(count));  
  142.                 return;  
  143.             } else {  
  144.                 CustomInteger preNumber = this.numberList.get(index - 1);  
  145.                 preNumber.setValue(preNumber.getValue() + count);  
  146.             }  
  147.         }  
  148.         if (index.intValue() == 0) {  
  149.             return;  
  150.         }  
  151.         arrangementList(index - 1);  
  152.     }  
  153.  
  154.     public List<Integer> getFormattedNumberList() {  
  155.         List<Integer> list = new ArrayList<Integer>();  
  156.         this.formattedNumberList();  
  157.         for (CustomInteger ci : this.numberList) {  
  158.             list.add(ci.getValue());  
  159.         }  
  160.         return list;  
  161.     }  
  162. }  
  163.  
  164. class CustomInteger {  
  165.     private Integer value;  
  166.  
  167.     public CustomInteger() {  
  168.     }  
  169.  
  170.     public CustomInteger(Integer value) {  
  171.         this.value = value;  
  172.     }  
  173.  
  174.     public Integer getValue() {  
  175.         return value;  
  176.     }  
  177.  
  178.     public void setValue(Integer value) {  
  179.         this.value = value;  
  180.     }  
  181.  
  182.     public String toString() {  
  183.         return String.valueOf(value);  
  184.     }  
  185. }  

測試代碼如下:
 

  1. public static void main(String[] args) {  
  2.         CustomDigit cd = new CustomDigit(750,27,1);  
  3.         for(Integer i :cd.getFormattedNumberList()){  
  4.             // 因爲從1開始,所以這邊只加上64,即從'A'輸出到'Z'  
  5.             System.out.print((char)(i+64));  
  6.         }  
  7.     } 

第一個參數爲10進制的數字,第二個參數爲進制,第三個參數爲該進制下的最小值,當最小值爲0時可省略第三個參數

上述測試程序輸出結果爲:

 

  1. ABV 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章