2016-06-14 82 views
-1

如何在私有数组中存储下表中的十六进制颜色?如何在数组中存储十六进制颜色

Name R G B 
BLACK 00 00 00 
NAVY 00 00 80 
BLUE 00 00 FF 

颜色的名称存储在公共枚举中。该数组也应该是类属性。

public enum COLOR_NAMES { 
    BLACK, NAVY, BLUE 
} 
+1

“数组也应该是类属性” - 这是什么意思? –

+0

您可以创建一个包含4个字段的类('COLOR_NAMES name','int r','int g','int b'),然后创建一个数组。 – Andreas

回答

2

你可以使用枚举来存储值你:

public enum COLORS { 
    BLACK(0x00, 0x00, 0x00), 
    NAVY(0x00, 0x00, 0x80), 
    BLUE(0x00, 0x00, 0xFF); 

    private int red; 
    private int green; 
    private int blue; 

    private COLORS(int red, int green, int blue) { 
     this.red = red; 
     this.green = green; 
     this.blue = blue; 
    } 

    public int getRed() { 
     return this.red; 
    } 

    public int getGreen() { 
     return this.green; 
    } 

    public int getBlue() { 
     return this.blue; 
    } 
} 
+0

enum和数组应该是分开的(enum public,array private access) – neox2811

+0

问题是,你不能在java中使用enum作为数组键。你可以使用我猜的地图,但为什么colorvalues需要是私人的? – tkausl

+0

这是一项任务 – neox2811