Creating an array while passing it as an argument in Java

后端 未结 4 687
没有蜡笔的小新
没有蜡笔的小新 2020-12-18 19:09

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

4条回答
  •  离开以前
    2020-12-18 20:04

    Following @Dave's suggest I would use a vararg

    QWERTY(1, 6, 0.5, 1.3, 23.1);
    DVORAK(5, 91, 0.1, 0.2, 4.3, 1.1);
    CHEROKEE(2, 11, 22.0);
    
    private final int number, thingy;
    private final double[] theArray;
    
    private KeyboardStuff(int number, int thingy, double... theArray) {
        // do things
    }
    

    It is pretty rare that using a float is better than using a double. double has less rounding error and only uses 4 more bytes.

提交回复
热议问题