Create an array with n copies of the same value/object?

后端 未结 7 1717
刺人心
刺人心 2020-12-16 11:29

I want to create an array of size n with the same value at every index in the array. What\'s the best way to do this in Java?

For example, if n

7条回答
  •  情深已故
    2020-12-16 11:56

    You can try it with:

    boolean[] array = new boolean[5];
    Arrays.fill(array, false);
    

    Second method with manual array fill:

    boolean[] array = new boolean[] {false, false, false, false, false};
    

提交回复
热议问题