Java .fill()
Published May 25, 2022
Contribute to Docs
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:
onezerozerozerozerosix
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours