Java.util.Arrays 类

java.util.Arrays.fill()方法用于将指定的double值赋给指定的每个元素指定双精度数组的范围。

语法

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

参数

a 指定要填充的数组。
fromIndex 指定第一个元素(含)的索引
toIndex 指定要填充的最后一个元素(不包括)的索引指定的值。
val 指定要存储在数组所有元素中的值。

返回值

void类型。

异常

  • 抛出IllegalArgumentException ,如果 fromIndex > toIndex。
  • 抛出 ArrayIndexOutOfBoundsException,如果 fromIndex < 0 或 toIndex > a.length。

示例:

在下面的示例中,java.util.Arrays.fill() 方法用于使用给定双精度数组的指定范围填充指定的双精度值。

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //创建双精度数组
    double MyArr[] = {10.1, 2.34, -3.6, 35.01, 56.23};

    //打印数组
    System.out.print("MyArr contains:"); 
    for(double i: MyArr)
      System.out.print(" " + i);

    //填充数组的指定范围
    //0.0 双精度值
    Arrays.fill(MyArr, 1, 4, 0.0);

    //打印数组
    System.out.print("\nMyArr contains:"); 
    for(double i: MyArr)
      System.out.print(" " + i);   
  }
}

上述代码的输出将是:

MyArr contains: 10.1 2.34 -3.6 35.01 56.23
MyArr contains: 10.1 0.0 0.0 0.0 56.23