Arrays.fill(Object[], int, int, Object) in Java

Arrays.fill(Object[], int, int, Object): This method is available in java.util.Arrays class of Java.

Syntax:

void java.util.Arrays.fill(Object[] a, int fromIndex, int toIndex, Object val)

This method takes four arguments one of type Object array, the other two are of type int, and the other one of type Object as its parameters. This method assigns the specified Object reference to each element of the specified range of the specified array of Objects.

Parameters: Four parameters are required for this method.

a: the array to be filled.

fromIndex: the index of the first element (inclusive) to be filled with the specified value.

toIndex: the index of the last element (exclusive) to be filled with the specified value.

val: the value to be stored in all elements of the array.

Returns: NA

Throws:

1. IllegalArgumentException - if fromIndex > toIndex.

2. ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > a.length.

3. ArrayStoreException - if the specified value is not of a runtime type that can be stored in the specified array.

Approach 1: When no exceptions

Java

import java.util.Arrays;

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

        Integer a[] = new Integer[5];

        int val = 10;

        int fromIndex = 0, toIndex = 2;
        Arrays.fill(a, fromIndex, toIndex, val);

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

Output:

[10, 10, null, null, null]


Approach 2: IllegalArgumentException

Java

import java.util.Arrays;

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

        Integer a[] = new Integer[5];

        int val = 10;

        int fromIndex = 3, toIndex = 2;
        Arrays.fill(a, fromIndex, toIndex, val);

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

Output:

Exception in thread "main" java.lang.IllegalArgumentException: fromIndex(3) > toIndex(2) at java.base/java.util.Arrays.rangeCheck(Arrays.java:718) at java.base/java.util.Arrays.fill(Arrays.java:3452)



Approach 3: ArrayIndexOutOfBoundsException 

Java

import java.util.Arrays;

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

        Integer a[] = new Integer[5];

        int val = 10;

        int fromIndex = -1, toIndex = 2;
        Arrays.fill(a, fromIndex, toIndex, val);

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

Output:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1 at java.base/java.util.Arrays.rangeCheck(Arrays.java:722) at java.base/java.util.Arrays.fill(Arrays.java:3452)



Approach 4: ArrayStoreException 

Java

import java.util.Arrays;

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

        Integer a[] = new Integer[5];

        char val = 'a';

        int fromIndex = 0, toIndex = 2;
        Arrays.fill(a, fromIndex, toIndex, val);

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

Output:

Exception in thread "main" java.lang.ArrayStoreException: java.lang.Character at java.base/java.util.Arrays.fill(Arrays.java:3454)



No comments:

Post a Comment