Is there a clean way to declare a Java array with the same value for each element?

This is not very important, but I was curious if there is a way to write one single liner Java, perhaps using guava or something to fill the array with elements that have the same value. For example, for example, Arrays.getSameElementArray(new long[12], 42L);

+6
source share
2 answers

Yes,

 long[] arr = new long[12]; Arrays.fill(arr, 42L); 
+21
source

You can do it:

 long[] values = new long[12]; Arrays.fill(values, 42l); 
+6
source

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


All Articles