Java Utility Library

Java Arrays - fill() Method



The java.util.Arrays.fill() method is used to assign the specified char value to each element of the specified array of chars.

Syntax

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

Parameters

a Specify the array to be filled.
val Specify the value to be stored in all elements of the array.

Return Value

void type.

Exception

NA.

Example:

In the example below, the java.util.Arrays.fill() method is used to fill the char array with specified char value.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a char array
    char MyArr[] = {'a', 'e', 'i', 'o', 'u'};

    //printing array
    System.out.print("MyArr contains:"); 
    for(char c: MyArr)
      System.out.print(" " + c);

    //fill the array with 'a' char value
    Arrays.fill(MyArr, 'a');

    //printing array
    System.out.print("\nMyArr contains:"); 
    for(char c: MyArr)
      System.out.print(" " + c);   
  }
}

The output of the above code will be:

MyArr contains: a e i o u
MyArr contains: a a a a a

❮ Java.util - Arrays