Java Arrays - fill() Method
The java.util.Arrays.fill() method is used to assign the specified float value to each element of the specified range of the specified array of floats.
Syntax
public static void fill(float[] a, int fromIndex, int toIndex, float val)
Parameters
a |
Specify the array to be filled. |
fromIndex |
Specify the index of the first element (inclusive) to be filled with the specified value. |
toIndex |
Specify the index of the last element (exclusive) to be filled with the specified value. |
val |
Specify the value to be stored in all elements of the array. |
Return Value
void type.
Exception
- Throws IllegalArgumentException, if fromIndex > toIndex.
- Throws ArrayIndexOutOfBoundsException, if fromIndex < 0 or toIndex > a.length.
Example:
In the example below, the java.util.Arrays.fill() method is used to fill a specified range of a given float array with a specified float value.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a float array float MyArr[] = {10.1f, 2.6f, -3.6f, 35f, 56f}; //printing array System.out.print("MyArr contains:"); for(float i: MyArr) System.out.print(" " + i); //fill the specified range of the array //with 0f float value Arrays.fill(MyArr, 1, 4, 0f); //printing array System.out.print("\nMyArr contains:"); for(float i: MyArr) System.out.print(" " + i); } }
The output of the above code will be:
MyArr contains: 10.1 2.6 -3.6 35.0 56.0 MyArr contains: 10.1 0.0 0.0 0.0 56.0
❮ Java.util - Arrays