How to initialize array in java when the class constructor has parameters?

后端 未结 4 952
耶瑟儿~
耶瑟儿~ 2021-02-04 09:28

I have this class constructor:

public Category(int max){
...
}

The thing is, I want to make an array of this class, how do I initialize it?

4条回答
  •  猫巷女王i
    2021-02-04 10:24

    You can also do that in-line - make both the array and populate it with values initiated with their constructors at once. Suppose you have a class called Field that has a constructor taking two parameters and you want to construct an array of these ...

    Field[] fields = new Field[]{
        new Field(1, "Record_Type"),
        new Field(3, "Record_SubType"),
        new Field(6, "Row_Number"),
        ...
    };
    

提交回复
热议问题