Is there a way to initialise an array of integers (or possibly any array) to a constant value other than zero (or null) which are the defaults, without a for loop?
Ideally I am looking for a function like "ones" in matlab, which is not only neater but also more efficient.
Arrays.fill()
is the method you're after. (Although internally it still uses a for loop, so unlike System.arrayCopy()
, it isn't any faster.)
P.s.: Arrays
, and its collection-based counterpart Collections
are two extremely useful classes in general.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With