How to properly initialize an array in Java with a constant value

Is there a way to initialize an array of integers (or, possibly, any array), to a constant value other than zero (or null), which are the default values, without a for loop?

Ideally, I am looking for a function like "one" in Matlab, which is not only neat, but also more efficient.

+4
source share
1 answer

Arrays.fill() is the method you use. (Although internally it still uses a for loop, so unlike System.arrayCopy() , it is not faster.)

Ps: Arrays , and his collector colleague Collections are two extremely useful classes in general.

+8
source

Source: https://habr.com/ru/post/1394871/


All Articles