Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an array while passing it as an argument in Java

Is there a way to create an array of objects as part of a constructor or method? I'm really not sure how to word this, so I've included an example. I have an enum, and one of the fields is an array of numbers. Here is what I tried:

public enum KeyboardStuff {

    QWERTY(1, {0.5f, 1.3f, 23.1f}, 6);
    DVORAK(5, {0.1f, 0.2f, 4.3f, 1.1f}, 91);
    CHEROKEE(2, {22.0f}, 11);

    private int number, thingy;
    private float[] theArray;

    private KeyboardStuff(int i, float[] anArray, int j) {
        // do things
    }

}

The compiler says that the brackets { } are invalid and should be removed. Is there a way I can pass an array as an argument without creating an array of objects beforehand?

like image 226
Tanaki Avatar asked Sep 25 '11 18:09

Tanaki


1 Answers

You can try with new float[] { ... }.

public enum KeyboardStuff {

    QWERTY(1, new float[] {0.5f, 1.3f, 23.1f}, 6);
    DVORAK(5, new float[] {0.1f, 0.2f, 4.3f, 1.1f}, 91);
    CHEROKEE(2, new float[] {22.0f}, 11);

    private int number, thingy;
    private float[] theArray;

    private KeyboardStuff(int i, float[] anArray, int j) {
        // do things
    }

}
like image 191
Vivien Barousse Avatar answered Oct 02 '22 02:10

Vivien Barousse