Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在数组中存储十六进制颜色_Java_Arrays - Fatal编程技术网

Java 如何在数组中存储十六进制颜色

Java 如何在数组中存储十六进制颜色,java,arrays,Java,Arrays,如何将下表中的十六进制颜色存储在专用数组中 Name R G B BLACK 00 00 00 NAVY 00 00 80 BLUE 00 00 FF 颜色的名称存储在公共枚举中。数组也应该是class属性 public enum COLOR_NAMES { BLACK, NAVY, BLUE } 您可以使用枚举为您存储值: public enum COLORS { BLACK(0x00, 0x00, 0x00), NAVY(

如何将下表中的十六进制颜色存储在专用数组中

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

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

public enum COLOR_NAMES {
    BLACK, NAVY, BLUE
}

您可以使用枚举为您存储值:

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

枚举和数组应该分开(枚举公共,数组私有访问)。问题是,在java中不能将枚举用作数组密钥。我想你可以使用一个映射,但是为什么颜色值需要是私有的呢?这是一个赋值“数组也应该是class属性”-这意味着什么?你创建一个包含4个字段的类(
COLOR\u name name
int r
int g
int b
),然后创建一个数组。