Fill a Double Array in Java
Declaration :
1 |
public static void fill(double[] a, double val) |
Explanation :
Purpose | The java.util.Arrays.fill(double[] a double val) method assigns the specified double value to each element of the specified array of doubles. |
Parameters | a ===> This is the array to be filled. |
val ===> This is the value to be stored in all elements of the array. | |
Return Value | This method does not return any value. |
Exception | NA |
Java Program : Example
Below example will explain java.util.Arrays.fill() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.c4learn; import java.util.Arrays; public class ArrayDemo { public static void main(String[] args) { // Initialize double array double arr[] = new double[] { 10.2, 15.2, 20.2, 25.2, 30.2 }; // Print the original array System.out.println("Original Array : "); for (double val : arr) { System.out.println("Value = " + val); } // Use fill to place 35.2 Arrays.fill(arr, 35.2); // Print the new array System.out.println("\nNew Array is : "); for (double val : arr) { System.out.println("New element is " + val); } } } |
Output of Program :
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Original Array : Value = 10.2 Value = 15.2 Value = 20.2 Value = 25.2 Value = 30.2 New Array is : New element is 35.2 New element is 35.2 New element is 35.2 New element is 35.2 New element is 35.2 |