在Java中将其作为参数传递时创建数组 [英] Creating an array while passing it as an argument in Java

查看:144
本文介绍了在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?

推荐答案

你可以试试 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
    }

}

这篇关于在Java中将其作为参数传递时创建数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆