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
List copies = Collections.nCopies(copiesCount, value);
javadoc here.
This is better than the 'Arrays.fill' solution by several reasons:
And lists are cooler than arrays :) But if you really-really-really want an array – then you can do the following:
Integer[] copies = Collections.nCopies(copiesCount, value)
.toArray(new Integer[copiesCount]);