Java Collection Tutorial - Java Arrays.fill(byte[] a, int fromIndex, int toIndex, byte val)








Syntax

Arrays.fill(byte[] a, int fromIndex, int toIndex, byte val) has the following syntax.

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

Example

In the following code shows how to use Arrays.fill(byte[] a, int fromIndex, int toIndex, byte val) method.

//from w  w  w .j  a v a  2 s .c  o m

import java.util.Arrays;

public class Main {

  public static void main(String[] args) {
    
    // initializing byte array
    byte arr[] = new byte[] {1, 6, 3, 2, 9};
    
    System.out.println(Arrays.toString(arr));

    // using fill for placing 64 from index 2 to 4
    // converting int to byte
    Arrays.fill(arr, 2, 4, (byte)64);

    System.out.println(Arrays.toString(arr));
  }
}

The code above generates the following result.