Java Utility Library

Java Arrays - fill() Method



The java.util.Arrays.fill() method is used to assign the specified double value to each element of the specified array of doubles.

Syntax

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

Parameters

a Specify the array to be filled.
val Specify the value to be stored in all elements of the array.

Return Value

void type.

Exception

NA.

Example:

In the example below, the java.util.Arrays.fill() method is used to fill the double array with specified double value.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a double array
    double MyArr[] = {10.1, 2.34, -3.6, 35.01, 56.23};

    //printing array
    System.out.print("MyArr contains:"); 
    for(double i: MyArr)
      System.out.print(" " + i);

    //fill the array with 0.0 double value
    Arrays.fill(MyArr, 0.0);

    //printing array
    System.out.print("\nMyArr contains:"); 
    for(double i: MyArr)
      System.out.print(" " + i);   
  }
}

The output of the above code will be:

MyArr contains: 10.1 2.34 -3.6 35.01 56.23
MyArr contains: 0.0 0.0 0.0 0.0 0.0

❮ Java.util - Arrays