What does the method fill(obj[], object val) do?


The fill(Object[] a, Object val) method of the java.util.Arrays class assigns the specified Object reference to each element of the specified array of Objects.

Example

import java.util.Arrays;
public class ArrayDemo {
   public static void main(String[] args) {
      Object arr[] = new Object[] {10.5, 5.6, 4.7, 2.9, 9.7};
      System.out.println("Actual values: ");
      for (Object value : arr) {
         System.out.println("Value = " + value);
      }
      Arrays.fill(arr, 12.2);
      System.out.println("New values after using fill() method: ");
      for (Object value : arr) {
         System.out.println("Value = " + value);
      }
   }
}

Output

Actual values:
Value = 10.5
Value = 5.6
Value = 4.7
Value = 2.9
Value = 9.7
New values after using fill() method:
Value = 12.2
Value = 12.2
Value = 12.2
Value = 12.2
Value = 12.2

Sharon Christine
Sharon Christine

An investment in knowledge pays the best interest

Updated on: 13-Mar-2020

108 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements