Java Arrays Fill

We love to work with arrays because they are simple yet very powerful data structure in Java Language. It is also very intuitive and easy to use. If our needs does not require too much dynamic sizes, then arrays are just fine. But the first problem we encounter in working with arrays is how to fill it with initial values. Specially if the size of an array is created through a variable - meaning variable size that we don't know initially. And say we need to initialize the entire thing with a specific value, or only a portion of the array. The Arrays.fill() method is a good fit for this need. In this post we try to explore how to use Java Arrays.fill() method in different scenarios.

Syntax for Filling All

Before we dig any deeper, it is useful to check the syntax of Arrays.fill method. See below for syntax examples taken from Java documentation here https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html . Below is the syntax for long.

public static void fill(long[] a, long val)

And here is for the type int.

public static void fill(int[] a, int val)
Below is the syntax for short.
public static void fill(short[] a, short val)
And here is for the type char for characters.
public static void fill(char[] a, char val)
Below is the syntax for byte.
public static void fill(byte[] a, byte val)
And here is for the type boolean if we need logical array.
public static void fill(boolean[] a, boolean val)
Below is the syntax for double.
public static void fill(double[] a, double val)
And also float in case.
public static void fill(float[] a, float val)
And lastly, syntax for generic catch all Object, because all class came from Object.
public static void fill(Object[] a, Object val)

The description is the same for all that this method assigns the specified parameter to each element of the specified array of item. Where a is the array to be filled and val is the value to be assigned to all items in the array.

Syntax for Range Fill

Below is an alternate syntax if we don't want to fill everything but only a specific range of the array. Which are denoted by the fromIndex and toIndex. See below syntax.
public static void fill(long[] a, int fromIndex, int toIndex, long val)

And here is for the type int.

public static void fill(int[] a, int fromIndex, int toIndex, int val)
Below is the syntax for short.
public static void fill(short[] a, int fromIndex, int toIndex, short val)
And here is for the type char for characters.
public static void fill(char[] a, int fromIndex, int toIndex, char val)
Below is the syntax for byte.
public static void fill(byte[] a, int fromIndex, int toIndex, byte val)
And here is for the type boolean if we need logical array.
public static void fill(boolean[] a, int fromIndex, int toIndex, boolean val)
Below is the syntax for double.
public static void fill(double[] a, int fromIndex, int toIndex, double val)
And also float in case.
public static void fill(float[] a, int fromIndex, int toIndex, float val)
And lastly, syntax for generic catch all Object, because all class came from Object.
public static void fill(Object[] a, int fromIndex, int toIndex, Object val)

The doc says the same for all versions that this method assigns the specified value to each element of the specified range of the specified array. The range to be filled extends from index fromIndex, inclusive, to index toIndex, exclusive.

Arrays Fill All Empty Array

Sometimes we create an array with no initial value. So we can set initial value to same value using the fill method. See below:

import java.util.Arrays;
/**
 * An example program that fills an empty array with same value for all elements.
 */
public class ExampleProgram {
    public static void main(String[] args) {
        int[] myArray = new int[10];
        Arrays.fill(myArray, 10);
        System.out.println("Array contents:" + Arrays.toString(myArray));
    }
}

And since we created an array of 10 integers that were not initialized, everything after the fill becomes 10 and thus we can below output:

Array contents:[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]

Arrays Fill Non Empty Array

If we modify the example to contain an initialized array, those values will be overriden by Arrays.fill() method.

import java.util.Arrays;
/**
 * An example program that fills an initialized array.
 */
public class ExampleProgram {
    public static void main(String[] args) {
        int[] myArray = {5, 6, 7, 8};
        Arrays.fill(myArray, 12);
        System.out.println("Array contents:" + Arrays.toString(myArray));
    }
}
We get all 4 elements to have value of 12.
Array contents:[12, 12, 12, 12]

Arrays Range Fill Empty Array

If we don't want to fill every element in an array, we can choose to just range fill of specific from and to index. Below is an example:
import java.util.Arrays;
/**
 * An example program that range fill an empty array.
 */
public class ExampleProgram {
    public static void main(String[] args) {
        int[] myArray = new int[10];
        Arrays.fill(myArray, 3, 5, 10);
        System.out.println("Array contents:" + Arrays.toString(myArray));    }
}
So since the array is not initialized, but since it is array of primitive int, we assume all elements get 0 value initially. And since we set fromIndex to be 3 and toIndex to be 5, we fill index 3 and 4 with the value 10. Index 5 is not included because toIndex is exclusive. And so we get below output:
Array contents:[0, 0, 0, 10, 10, 0, 0, 0, 0, 0]

Arrays Range Fill Non Empty Array

And if our array already have initial values, the behavior is just the same.
import java.util.Arrays;
/**
 * An example program that range fill a non empty array.
 */
public class ExampleProgram {
    public static void main(String[] args) {
        int[] myArray = {3, 6, 2, 1, 5, 8, 9, 15, 12, 13, 8};
        Arrays.fill(myArray, 2, 6, 10);
        System.out.println("Array contents:" + Arrays.toString(myArray));    }
}

Since fromIndex is 2 and toIndex is 6, we will index 2, 3, 4, and 5. Hence we get below output when we run this program/

Array contents:[3, 6, 10, 10, 10, 10, 9, 15, 12, 13, 8]