Java Initialize an int array in a constructor

后端 未结 6 1202
失恋的感觉
失恋的感觉 2020-12-07 14:11

I have a class and in that class I have this:

 //some code
 private int[] data = new int[3];
 //some code

Then in my constructor:



        
6条回答
  •  南笙
    南笙 (楼主)
    2020-12-07 14:58

    private int[] data = new int[3];
    

    This already initializes your array elements to 0. You don't need to repeat that again in the constructor.

    In your constructor it should be:

    data = new int[]{0, 0, 0};
    

提交回复
热议问题