HOWTO · Java

How to Initialize All Array Elements to Zero in Java

This article discusses the methods you can do to initialize all array elements to zero in Java.

On this page

A newly allocated Java array of a primitive numeric type is initialized with zero-valued elements, so no extra step is needed for a fresh array. To reset an existing array, use Arrays.fill() or assign each element explicitly. Arrays of reference types, such as Integer[], instead start with null elements.
Array initialization is covered as part of the core data path in the Java Tutorial.

Initialize Array Elements to Zero by Using a for Loop

A for loop assigns a value to each index in an existing array. The loop starts at index 0 and stops before numbers.length, so every valid element is visited.

This complete Java program applies the techniques to short arrays and prints each result:

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ArrayInitialization {
  public static void main(String[] args) {
    int[] loopValues = {4, 8, 12};
    for (int i = 0; i < loopValues.length; i++) {
      loopValues[i] = 0;
    }
    System.out.println("for loop: " + Arrays.toString(loopValues));

    int[] filledValues = {4, 8, 12};
    Arrays.fill(filledValues, 0);
    System.out.println("fill: " + Arrays.toString(filledValues));

    List<Integer> repeatedValues = Collections.nCopies(3, 0);
    Integer[] boxedValues = repeatedValues.toArray(new Integer[0]);
    System.out.println("boxed values: " + Arrays.toString(boxedValues));

    int[] numbers = {4, 8, 12};
    int[] oldReference = numbers;
    numbers = new int[numbers.length];
    System.out.println("replacement: " + Arrays.toString(numbers));
    System.out.println("old reference: " + Arrays.toString(oldReference));

    int[] generatedValues = {4, 8, 12};
    Arrays.setAll(generatedValues, index -> 0);
    System.out.println("setAll: " + Arrays.toString(generatedValues));

    int[] newValues = new int[3];
    System.out.println("new array: " + Arrays.toString(newValues));
  }
}

The program prints:

for loop: [0, 0, 0]
fill: [0, 0, 0]
boxed values: [0, 0, 0]
replacement: [0, 0, 0]
old reference: [4, 8, 12]
setAll: [0, 0, 0]
new array: [0, 0, 0]

Initialize Array Elements to Zero by Using the fill() Method in Java

Arrays.fill() writes the supplied value into every element of the array. For a primitive numeric array, Arrays.fill(numbers, 0) resets the existing array in place. The same method also has overloads for filling a selected range when only part of an array should change.

Initialize Array Elements to Zero by Using the nCopies() Method

Collections.nCopies(length, 0) returns an immutable List<Integer> containing repeated boxed values. Converting that list with toArray(new Integer[0]) produces an Integer[]; it does not produce an int[] or reset an existing primitive array. Use it when a list or boxed array is the desired result, rather than as a substitute for modifying the original array.

Initialize Array Elements to Zero by Using Reassignment in Java

Assigning new int[numbers.length] makes numbers refer to a new array whose elements have their default values. Any other variable that refers to the original array continues to see its previous contents. Reassignment replaces one reference; it does not clear the old array for every reference to it.

Initialize Array Elements to Zero by Using Arrays.setAll()

Arrays.setAll() calls an index-based generator for each element and stores the generated value in the array. The example uses index -> 0 to assign the same value at every index. This method is useful when a value depends on its index; Arrays.fill() is more direct when every element receives one constant.

Conclusion

A new primitive numeric array is already initialized with zeros. For an existing array that must be reset, Arrays.fill() is a direct choice; a for loop makes the per-element assignment explicit, and Arrays.setAll() supports index-based values. Collections.nCopies() creates repeated boxed values in a list, while reassignment creates a new array and leaves any other references to the old one unchanged.