Java Arrays.fill(int[] a, int fromIndex, int toIndex, int val)

Syntax

Arrays.fill(int[] a, int fromIndex, int toIndex, int val) has the following syntax.

public static void fill(int[] a, int fromIndex, int toIndex, int val)

Example

In the following code shows how to use Arrays.fill(int[] a, int fromIndex, int toIndex, int val) method.


//from ww w  . ja  v a 2s  . co  m

import java.util.Arrays;

public class Main {

  public static void main(String[] args) {
    
    // initializing int array
    int arr[] = new int[] {1, 2, 3, 4, 5};
    
    System.out.println(Arrays.toString(arr));

    // using fill for placing 18 from index 2 to 4
    Arrays.fill(arr, 2, 4, 18);

    System.out.println(Arrays.toString(arr));
  }
}

The code above generates the following result.