0

In c++, I can do

vector<int> arr(10, 100); // arr with Size 10 and default value 100. 

Is there a similar way (one-liner) to initialize java array with a size and default value?

derek
  • 8,100
  • 10
  • 47
  • 79

1 Answers1

1

There is no built-in function. In Java, arrays are always initialized with 0 or false or null, according to type.

You can import java.util.Arrays; and then do:

int[] arr = new int[10];
Arrays.fill(arr, 100);
user207421
  • 298,294
  • 41
  • 291
  • 462
Dorian Gray
  • 2,781
  • 1
  • 8
  • 25