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
  • 6,738
  • 10
  • 39
  • 69

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
  • 289,834
  • 37
  • 266
  • 440
Dorian Gray
  • 2,591
  • 1
  • 7
  • 25