Table of Content

Fill an Object Array using Index in Java

Declaration :

Explanation :

Purpose The java.util.Arrays.fill(object[] a int fromIndex int toIndex object val) method assigns the specified Object reference to each element of the specified range of the specified array of objects. The range to be filled extends from index fromIndex inclusive to index toIndex exclusive.(If fromIndex==toIndex the range to be filled is empty.).
Parameters a ===> This is the array to be filled.
fromIndex ===> This is the index of the first element (inclusive) to be filled with the specified value.
toIndex ===> This is the index of the last element (exclusive) to be filled with the specified value.
val ===> This is the value to be stored in all elements of the array.
Return Value This method does not return any value.
Exception ArrayIndexOutOfBoundsException — if fromIndex < 0 or toIndex > a.length
,IllegalArgumentException — if fromIndex > toIndex
,ArrayStoreException — if the specified value is not of a runtime type that can be stored in the specified array.

Java Program : Example

Below example will explain java.util.Arrays.fill() method.

Output of Program :