Fill a Float Array in Java
Declaration :
1 |
public static void fill(float[] a, float val) |
Explanation :
Purpose | The java.util.Arrays.fill(float[] a float val) method assigns the specified float value to each element of the specified array of floats. |
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 float array float arr[] = new float[] { 10f, 20f, 30f, 40f, 50f }; // Print the original array System.out.println("Original Array : "); for (float val : arr) { System.out.println("Original element is " + val); } // Use fill to place 25 Arrays.fill(arr, 25f); // Print the new array System.out.println("\nNew Array is : "); for (float 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 : Original element is 10.0 Original element is 20.0 Original element is 30.0 Original element is 40.0 Original element is 50.0 New Array is : New element is 25.0 New element is 25.0 New element is 25.0 New element is 25.0 New element is 25.0 |