Java Arrays.fill() Method
Example
Fill all the elements in an array with a "Kiwi" value:
String[] fruits = {"Banana", "Orange", "Apple", "Mango"};
Arrays.fill(fruits, "Kiwi");
Definition and Usage
The fill()
method fills an array with a specified value.
Note: The value must be of the same data type as the array.
Tip: Start and end position can be specified. If not, all elements will be filled.
Syntax
Arrays.fill(array, value)
Arrays.fill(array, start, end, value)
Parameter Values
Parameter | Description |
---|---|
array | Required. The array to be filled |
start | Optional. The index position of the first element (inclusive) to be filled |
end | Optional. The index position of the last element (exclusive) to be filled |
value | Required. The value to fill in the array |
Technical Details
Returns: | No return value |
---|---|
Java version: | 1.2 (java.util ) |
More Examples
Example
Fill the last two elements:
String[] fruits = {"Banana", "Orange", "Apple", "Mango"};
Arrays.fill(fruits, 2, 4, "Kiwi");
Related Pages
❮ Arrays Methods