Java.util.Arrays.fill(short[], short) Method


Advertisements


Description

The java.util.Arrays.fill(short[] a, short val) method assigns the specified short value to each element of the specified array of shorts.

Declaration

Following is the declaration for java.util.Arrays.fill() method

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

Parameters

  • a − This is the array to be filled.

  • val − This is the value to be stored in all elements of the array.

Return Value

This method does not return value.

Exception

NA

Example

The following example shows the usage of java.util.Arrays.fill() method.

Live Demo
package com.tutorialspoint;

import java.util.Arrays;

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

      // initializing short array
      short arr[] = new short[] {1, 5, 3, 2, 13};

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

      // using fill for placing 23
      Arrays.fill(arr, (short) 23);

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

Let us compile and run the above program, this will produce the following result −

Actual values:
Value = 1
Value = 5
Value = 3
Value = 2
Value = 13
New values after using fill() method:
Value = 23
Value = 23
Value = 23
Value = 23
Value = 23

java_util_arrays.htm

Advertisements