.fill()

The .fill() method of the Arrays class fills a given array with a given value. The value must match the data type of the array.

Syntax

Arrays.fill(array, value)

Arrays.fill(array, start, end, value)

Here, array, is the array being filled and value is the value being filled in the array. start and end indices can also be specified and the fill will be confined to the given range.

Example

The following example creates an array and then fills part of it.

import java.util.*;
public class Example {
public static void main(String args[]) {
String[] a = {"one", "two", "three", "four", "five", "six"};
Arrays.fill(a, 1, 5, "zero");
for (int i = 0; i < 6; i++) {
System.out.println(a[i]);
}
}
}

This will output the following:

one
zero
zero
zero
zero
six

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Java on Codecademy