目录

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

描述 (Description)

java.util.Arrays.fill(short[] a, int fromIndex, int toIndex, short val)方法将指定的short值分配给指定short类数组的指定范围的每个元素。 要填充的范围从索引fromIndex(包括)扩展到索引toIndex(不包括)。 (If fromIndex==toIndex, the range to be filled is empty.)

声明 (Declaration)

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

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

参数 (Parameters)

  • a - 这是要填充的数组。

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

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

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

返回值 (Return Value)

此方法不返回任何值。

异常 (Exception)

  • ArrayIndexOutOfBoundsException - 如果fromIndex“0或toIndex”a.length

  • IllegalArgumentException - 如果fromIndex“toIndex

例子 (Example)

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

package com.iowiki;
import java.util.Arrays;
public class ArrayDemo {
   public static void main(String[] args) {
      // initializing short array
      short arr[] = new short[] {2, 5, 13, 29, 97};
      // let us print the values
      System.out.println("Actual values: ");
      for (short value : arr) {
         System.out.println("Value = " + value);
      }
      // using fill for placing 19 from index 1 to 3
      Arrays.fill(arr, 1, 3, 19);
      // let us print the values
      System.out.println("New values after using fill() method: ");
      for (short value : arr) {
         System.out.println("Value = " + value);
      }
   }
} 

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

Actual values:
Value = 2
Value = 5
Value = 13
Value = 29
Value = 97
New values after using fill() method:
Value = 2
Value = 19
Value = 19
Value = 29
Value = 97
↑回到顶部↑
WIKI教程 @2018