Java.util.Arrays.fill()


描述

所述java.util.Arrays.fill(float[] a, int fromIndex, int toIndex, float val)方法分配指定的float值到指定的浮子的阵列的指定范围中的每个元素。要填充的范围从索引fromIndex(包括)扩展到索引toIndex,exclusive。(如果fromIndex == toIndex,则要填充的范围为空。)。

声明

以下是java.util.Arrays.fill()方法的声明

public static void fill(float[] a, int fromIndex, int toIndex, float val)

参数

a - 这是要填充的数组。

fromIndex - 这是要用指定值填充的第一个元素(包括)的索引。

toIndex - 这是要使用指定值填充的最后一个元素(不包括)的索引。

val - 这是要存储在数组的所有元素中的值。

返回值

此方法不返回任何值。

异常

ArrayIndexOutOfBoundsException − if fromIndex < 0 or toIndex > a.length

IllegalArgumentException − if fromIndex > toIndex

实例

以下示例显示了java.util.Arrays.fill()方法的用法。

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {

      // initializing float array
      float arr[] = new float[] {1f, 5f, 3f, 2f, 9f};

      // let us print the values
      System.out.println("Actual values: ");
      for (float value : arr) {
         System.out.println("Value = " + value);
      }

      // using fill for placing 12 from index 1 to 3
      Arrays.fill(arr, 1, 3, 12f);

      // let us print the values
      System.out.println("New values after using fill() method: ");
      for (float value : arr) {
         System.out.println("Value = " + value);
      }
   }
}

让我们编译并运行上面的程序,这将产生以下结果

Actual values:
Value = 1.0
Value = 5.0
Value = 3.0
Value = 2.0
Value = 9.0
New values after using fill() method:
Value = 1.0
Value = 12.0
Value = 12.0
Value = 2.0
Value = 9.0