java中枚举构造函数中作为参数的数组

java中枚举构造函数中作为参数的数组,java,collections,enums,composite-key,Java,Collections,Enums,Composite Key,我在double[]中有5个预定义值的数据集: A = {1.5, 1.8, 1.9, 2.3, 2.7, 3.0} B = {1.2, 1.8, 1.9, 2.4, 2.9, 3.1} . . E = {1.4, 1.7, 1.8, 1.9, 2.3, 2.9} 如何在枚举中表示它? 我试图将其编码为: private enum Solutions{ A(double[]), B(double[]), C(double[]), D(double[]), E(double[])

我在double[]中有5个预定义值的数据集:

A = {1.5, 1.8, 1.9, 2.3, 2.7, 3.0} 
B = {1.2, 1.8, 1.9, 2.4, 2.9, 3.1} 
.
.
E = {1.4, 1.7, 1.8, 1.9, 2.3, 2.9} 
如何在枚举中表示它? 我试图将其编码为:

private enum Solutions{
 A(double[]),
 B(double[]),
 C(double[]),
 D(double[]),
 E(double[]) ;

 private double[] val;

 private Solutions(double[] pVal){
   this.val = pVal;
 }

}
这可能吗

在java中,最好的数据类型或数据结构是什么? 除了上述双[]数组之外,用户还可以定义自己的自定义数组


请提供任何建议。

我将使用varargs作为您的潜在号码:

private Solutions(double... values) {
    this.val = values;
}
这将允许您传递任何可用值:

A(12.4, 42.4, 30.2, 1.3),
B(39.2, 230.3, 230.0),
//etc...

@Rogue的回答是正确的,非常简洁,但对于我们这些不知道如何将数组文本实例化为参数的人,或者非常不幸的是,您不能使用Java 5+(我对此深表怀疑)

你很接近。您只需要实例化双数组

private enum Solutions {
    A(new double[] {1.5, 1.8, 1.9, 2.3, 2.7, 3.0}),
    B(new double[] {1.2, 1.8, 1.9, 2.4, 2.9, 3.1}),
    C(new double[] {}),
    D(new double[] {}),
    E(new double[] {1.4, 1.7, 1.8, 1.9, 2.3, 2.9} ) ;

    private double[] val;

    private Solutions(double[] pVal) {
        this.val = pVal;
    }
}

谢谢你的回答,它们对我很有用。 到目前为止,我一直在研究这个问题,下面是一段代码,它可以接受enum的自定义值

public class X {
    private enum Solutions{
        A (new double [] {1.5, 1.8, 1.9, 2.3, 2.7, 3.0} ),
        B (new double [] {1.2, 1.8, 1.9, 2.4, 2.9, 3.1} ),
        C (new double [] {1.3, 1.7, 0.9, 1.4, 2.2, 3.1} ),
        D (new double [] {1.2, 1.4, 1.5, 2.6, 1.9, 3.1} ),
        E (new double [] {1.4, 1.7, 1.8, 1.9, 2.3, 2.9} ),
        CUSTOM (new double [] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0} );

        private double[] val;

         private Solutions (double[] pVal) {
           val = pVal;
         }

         public double[] getVal(){
             return this.val;
         }

         public void setVal(double[] pVal){
             val = pVal;
         }

    }

    public X() {
        // TODO Auto-generated constructor stub
    }

    public static void main(String[] args){
        Solutions a = Solutions.A;
        System.out.println("enum Solution A at index 0 is: " + a.getVal()[0] );

        Solutions custom = Solutions.CUSTOM;
        System.out.println("enum Solution Custom  at index 0 is: " + custom.getVal()[0] );

        double[] custArray = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
        custom.setVal(custArray);

        System.out.println("enum Solution Custom at index 0 after modification is: " + custom.getVal()[0] );

    }
}
我最后一个问题是: 是否有自动方法强制只接受长度为6(特定长度数组)的double[] 还是我自己去查

让我们说。。。是否有任何方法可以这样编写枚举的成员变量:

private double[6] val;
而不是

private double[] val;

枚举是编译时常量,因此如果您希望用户定义自己的数组,枚举绝对不是一种好方法。使用任何类型的集合都可以。Varargs是在Java5oh中引入的哇,真是个疏忽!谢谢你的更正。我已经编辑了答案。我仍然觉得我的答案对那些不知道如何将匿名数组作为参数传递的人很有用。