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
  • 9,358
  • 11
  • 53
  • 94

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
  • 305,947
  • 44
  • 307
  • 483
Dorian Gray
  • 2,913
  • 1
  • 9
  • 25