0Pricing
Java Academy · Lesson

Arrays.fill and copyOf

Fill and resize arrays.

Filling Arrays

Arrays.fill sets every element of an array to the same value. It modifies the array in place and is handy for initializing or resetting data.

A Basic Fill

Fill an entire array with one value.

import java.util.Arrays;

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

All lessons in this course

  1. Arrays.sort and Sorting
  2. Arrays.binarySearch
  3. Arrays.fill and copyOf
  4. Arrays.equals and toString
← Back to Java Academy