Skip to main content
Documents
Share
LinkedIn
Facebook
Twitter
Copy
Java keywordsIntroduction To JavaJava File HandlingJava Language BasicsJava ArraysJava Object-Oriented Programming

Java Array fill()

The Arrays.fill() method in Java is a utility function provided by the java.util.Arrays class. It is used to assign a specified value to each element of an array. This method is particularly useful for initializing arrays or resetting their values efficiently.

Usage

The Arrays.fill() method is available for different data types, including int, char, double, boolean, and more. It simplifies the process of populating arrays with a default or specific value.

Syntax

Arrays.fill(array, value);
Arrays.fill(array, fromIndex, toIndex, value);
  • array: The array to be filled.
  • value: The value to assign to each element.
  • fromIndex: The starting index (inclusive) for filling.
  • toIndex: The ending index (exclusive) for filling.

Examples

Example 1: Fill Entire Array

import java.util.Arrays;

public class FillArrayExample {
    public static void main(String[] args) {
        int[] numbers = new int[5];
        Arrays.fill(numbers, 10);
        System.out.println(Arrays.toString(numbers));
    }
}

In this example, the Arrays.fill() method is used to fill the entire numbers array with the value 10. The Arrays.toString() method is then used to print the array, resulting in [10, 10, 10, 10, 10].

Example 2: Fill Partial Array

import java.util.Arrays;

public class PartialFillArrayExample {
    public static void main(String[] args) {
        char[] chars = {'a', 'b', 'c', 'd', 'e'};
        Arrays.fill(chars, 1, 4, 'x');
        System.out.println(Arrays.toString(chars));
    }
}

Here, the Arrays.fill() method fills a portion of the chars array from index 1 to 3 (inclusive of 1, exclusive of 4) with the character 'x'. The resulting array is [a, x, x, x, e].

Example 3: Fill Boolean Array

import java.util.Arrays;

public class BooleanFillArrayExample {
    public static void main(String[] args) {
        boolean[] flags = new boolean[4];
        Arrays.fill(flags, true);
        System.out.println(Arrays.toString(flags));
    }
}

This example demonstrates filling a boolean array flags with the value true. The Arrays.fill() method sets each element of the array to true, resulting in [true, true, true, true].

Tips and Best Practices

  • Efficiency: Use Arrays.fill() for efficient initialization of arrays with a constant value, reducing the need for manual loops.
  • Index Bounds: Ensure that the fromIndex and toIndex are within the array's bounds to avoid ArrayIndexOutOfBoundsException.
  • Data Types: The method is overloaded to support various data types, so make sure to use the appropriate method for your array type.
  • Partial Filling: Utilize the partial filling capability to modify specific segments of an array without affecting other elements.
  • Default Initialization: Use Arrays.fill() to quickly reset an array to a default state, especially in algorithms requiring multiple iterations with fresh data.