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

后端 未结 4 936
耶瑟儿~
耶瑟儿~ 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条回答
  •  被撕碎了的回忆
    2021-02-04 10:18

    private Category[] categories = new Category[4];
    

    Will be instantiated with 4 null categories, you have to fill the content yourself later.
    Or you can try:

    private Category[] categories = {new Category(max), new Category(max), new Category(max), new Category(max)};
    

提交回复
热议问题