2014-12-08 48 views
0

我想給一個enum變量分配一個字節數組值。我能夠將一個字節分配給一個變量,但無法分配一個完整的字節數組。我怎樣才能做到這一點? (我希望我的問題是不是荒謬。)如何將一個字節數組分配給一個枚舉變量並將其打印出來?

public enum abc 
{ 
     a (new byte[] {0x11,0x22}, 
     b ((byte)0x17); 
     byte value; 
     byte[] val=new byte[2]; 
     private abc(byte[] val) 
     { 
      this.val=val; 
     } 
     private abc(byte value) 
     { 
      this.value=value; 
     } 
} 

現在如果我想打印abc.a ......它顯示我0,而不是11 22.And我想打印整個序列爲11月22日通過將所有的值存儲在一個數組中。我該怎麼做?(現在我的問題清楚了嗎?)

+0

爲什麼要這樣做?你想達到什麼目的? – 2014-12-08 09:51:33

+0

您能否通過「將一個字節分配給[n個變量]」來顯示代碼。因爲我不太清楚你的意思。也許如果我們能看到這些代碼,我們會更好地理解你想要做什麼。 – 5gon12eder 2014-12-08 10:06:48

+0

@Abinaya:'print'是什麼意思 - 是關於調用枚舉的'toString'方法? – home 2014-12-08 10:15:20

回答

3

類似這樣?

public enum CustomEnumConstructor { 
    Fibonacci(new int[]{1, 1, 2, 3, 5, 8, 13, 21}, new int[]{34, 55, 89, 144, 233, 377, 610}, 987), 
    SternBrocot(new int[]{1, 1, 2, 1, 3, 2, 3, 1, 4, 3}), ; 

    private final int[] array; 

    private CustomEnumConstructor(int[] array1, int[] array2, int value) { 
     int[] array12 = new int[array1.length + array2.length]; 
     System.arraycopy(array1, 0, array12, 0, array1.length); 
     System.arraycopy(array2, 0, array12, array1.length, array2.length); 

     array = new int[array12.length + 1]; 
     System.arraycopy(array12, 0, array, 0, array12.length); 
     array[array12.length] = value; 
    } 

    private CustomEnumConstructor(int[] array) { 
     this.array = array; 
    } 

    public int[] getArray() { 
     return array; 
    } 
} 
+0

我懷疑這可能是OP想要的,但說實話,你的標識符是非常令人不安的。我必須三次閱讀你的代碼,直到我明白它的意思。 – 5gon12eder 2014-12-08 10:09:09

+0

是的......但我仍然得到0作爲他們的值,而不是那些數組{1,2,3,4} – Abinaya 2014-12-08 10:09:50

+0

@ 5gon12eder標識符是非常令人不安的? – TacB0sS 2014-12-08 10:13:58

相關問題