Java Program to fill elements in a float array


Elements can be filled in a float array using the java.util.Arrays.fill() method. This method assigns the required float value to the float array in Java. The two parameters required are the array name and the value that is to be stored in the array elements.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.util.Arrays;
public class Demo {
   public static void main(String[] argv) throws Exception {
      float[] floatArray = new float[5];
      float floatValue = 8.5F;
      Arrays.fill(floatArray, floatValue);
      System.out.println("The float array content is: " + Arrays.toString(floatArray));
   }
}

Output

The float array content is: [8.5, 8.5, 8.5, 8.5, 8.5]

Now let us understand the above program.

First, the float array floatArray[] is defined. Then the value 8.5 is filled in the float array using the Arrays.fill() method. Finally, the float array is printed using the Arrays.toString() method. A code snippet which demonstrates this is as follows −

float[] floatArray = new float[5];
float floatValue = 8.5F;
Arrays.fill(floatArray, floatValue);
System.out.println("The float array content is: " + Arrays.toString(floatArray));

Updated on: 26-Jun-2020

164 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements