在 Java 中将数组作为参数传递时创建数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7547722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:26:11  来源:igfitidea点击:

Creating an array while passing it as an argument in Java

javaarraysparametersargumentsargument-passing

提问by Tanaki

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?

编译器说括号 { } 无效,应该删除。有没有一种方法可以将数组作为参数传递,而无需事先创建对象数组?

回答by Vivien Barousse

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

您可以尝试使用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
    }

}

回答by Peter Lawrey

Following @Dave's suggest I would use a vararg

按照@Dave 的建议,我会使用 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 floatis better than using a double. double has less rounding error and only uses 4 more bytes.

使用 afloat比使用 a 更好的情况非常罕见double。double 具有较少的舍入误差并且仅使用了 4 个以上的字节。

回答by Dave Newton

Is there a way you can pass an array without creating an array?

有没有办法在不创建数组的情况下传递数组?

No, but you could use varargsto make it mostly-invisible, although that intat the end might need to move.

不,但您可以使用varargs使其几乎不可见,尽管int最后可能需要移动。

回答by Arnout Engelen

If using Lists's instead of arrays is an option, future versions of Java might start supporting a 'collection literals' syntax which unfortunately doesn't seem to have made it into Java 8:

如果使用列表而不是数组是一种选择,那么 Java 的未来版本可能会开始支持“集合文字”语法,但不幸的是,Java 8 中似乎没有这种语法:

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 List<Float> values;

    private KeyboardStuff(int i, List<Float> values, int j) {
        // do things
    }

}