Question

I have 5 data sets of predefined values in double[]:

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} 

how can I represent it in enum? I am trying to coded it like:

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

 private double[] val;

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

}

is this possible?

or

what is the best datatype or data structure represent this in java? in addition to above double[] arrays, user could defined his own customized array.

any suggestion please.

Was it helpful?

Solution 2

@Rogue's answer is correct and very concise, but for those of us who don't know how to instantiate an array literal as an argument, or your very unfortunate and can't use Java 5+ (I highly doubt this)

You are very close. You just need to instantiate the double array

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

OTHER TIPS

I would use varargs for your potential numbers:

private Solutions(double... values) {
    this.val = values;
}

This would allow you to pass whatever usable values there are:

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

thanks for all answers, they are very useful for me. so far I worked on this issue and here is a code which can accept customized values for 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] );

    }
}

now my final question related to this issue is: is there automated way to force accepting only double[] with length = 6 (specific length array) or i have to check it by myself?

lets say ... is there any way to write member variable of enum like this:

private double[6] val;

instead of

private double[] val;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top