ray ray - 5 months ago 27
Java Question

Declare Initial Value of BigDecimal Array in Java

I want to declare a

BigDecimal
Array
with initial value
0
like this:

BigDecimal[] val = {0,0,0};


but it's not working. Please help me to know how to declare
BigDecimal
array
with initial value.

Answer

I would use Arrays.fill() as that will would for any number of zeros (or any other BigDecimal value you like) This works because BigDecimal is immutable, don't do this for mutable values ;)

BigDecimal[] val = new BigDecimal[N];
Arrays.fill(val, BigDecimal.ZERO);