fill() – fill array

Arrays.fill() is a static method in Java’s java.util.Arrays class that assigns a specified value to each element of an array. This method is particularly useful for initializing or resetting the contents of an array to a specific value.

Key Points of Arrays.fill()

  1. In-Place Modification:
    • The method modifies the original array by setting each element to the specified value.
    • It does not return a new array; instead, it operates directly on the provided array.
  2. Overloaded Methods:
    • Arrays.fill() is overloaded to handle different types of arrays, including primitive types (e.g., int, char, double) and object types.
  3. Range Specification:
    • You can fill the entire array or specify a subrange (from a starting index to an ending index) to be filled with the specified value.

Overloaded Methods

Here are the main overloaded variants of the Arrays.fill() method:

  • public static void fill(int[] a, int val)
  • public static void fill(long[] a, long val)
  • public static void fill(float[] a, float val)
  • public static void fill(double[] a, double val)
  • public static void fill(char[] a, char val)
  • public static void fill(byte[] a, byte val)
  • public static void fill(boolean[] a, boolean val)
  • public static void fill(short[] a, short val)
  • public static void fill(Object[] a, Object val)
  • public static void fill(int[] a, int fromIndex, int toIndex, int val)
  • public static void fill(long[] a, int fromIndex, int toIndex, long val)
  • public static void fill(float[] a, int fromIndex, int toIndex, float val)
  • public static void fill(double[] a, int fromIndex, int toIndex, double val)
  • public static void fill(char[] a, int fromIndex, int toIndex, char val)
  • public static void fill(byte[] a, int fromIndex, int toIndex, byte val)
  • public static void fill(boolean[] a, int fromIndex, int toIndex, boolean val)
  • public static void fill(short[] a, int fromIndex, int toIndex, short val)
  • public static void fill(Object[] a, int fromIndex, int toIndex, Object val)

Examples

Filling Primitive Type Arrays

import java.util.Arrays;

public class ArrayFillExample {
    public static void main(String[] args) {
        int[] intArray = new int[5];
        double[] doubleArray = new double[5];
        char[] charArray = new char[5];

        // Filling the entire array with a specified value
        Arrays.fill(intArray, 42);
        Arrays.fill(doubleArray, 3.14);
        Arrays.fill(charArray, 'A');

        // Printing filled arrays
        System.out.println("intArray: " + Arrays.toString(intArray)); // Output: [42, 42, 42, 42, 42]
        System.out.println("doubleArray: " + Arrays.toString(doubleArray)); // Output: [3.14, 3.14, 3.14, 3.14, 3.14]
        System.out.println("charArray: " + Arrays.toString(charArray)); // Output: [A, A, A, A, A]
    }
}

Filling Object Type Arrays

import java.util.Arrays;

public class ArrayFillExample {
    public static void main(String[] args) {
        String[] stringArray = new String[5];

        // Filling the entire array with a specified value
        Arrays.fill(stringArray, "hello");

        // Printing filled array
        System.out.println("stringArray: " + Arrays.toString(stringArray)); // Output: [hello, hello, hello, hello, hello]
    }
}

Filling a Subrange of an Array

import java.util.Arrays;

public class ArrayFillExample {
    public static void main(String[] args) {
        int[] intArray = new int[10];

        // Filling a subrange of the array with a specified value
        Arrays.fill(intArray, 2, 7, 99);

        // Printing the array after filling
        System.out.println("intArray: " + Arrays.toString(intArray)); // Output: [0, 0, 99, 99, 99, 99, 99, 0, 0, 0]
    }
}

Important Considerations

  • Boundary Checks: If the fromIndex is greater than the toIndex, an IllegalArgumentException is thrown. If either fromIndex or toIndex are out of range (less than 0 or greater than the array length), an ArrayIndexOutOfBoundsException is thrown.
  • Performance: Arrays.fill() is generally efficient and can be used to quickly initialize or reset large arrays.
  • Object Types: When filling an array of objects, the same reference is used for each element, meaning that all elements will point to the same object.

In summary, Arrays.fill() is a versatile and efficient method for initializing or resetting arrays in Java. It provides various overloaded versions to handle different data types and offers flexibility by allowing you to fill entire arrays or specific subranges.