> For the complete documentation index, see [llms.txt](https://lintcode-solutions.gitbook.io/project/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lintcode-solutions.gitbook.io/project/oj-review/aug-8-2018.md).

# Aug 8, 2018

## `Arrays.fill()` in Java with Examples

`java.util.Arrays.fill()` method is in [java.util.Arrays class](https://www.geeksforgeeks.org/array-class-in-java/). This method assigns the specified data type value to each element of the specified range of the specified array.

```java
Syntax:
// Makes all elements of a[] equal to "val"
public static void fill(int[] a, int val)

// Makes elements from from_Index (inclusive) to to_Index
// (exclusive) equal to "val"
public static void fill(int[] a, int from_Index, int to_Index, int val)

This method doesn't return any value.
```

```java
Exceptions it Throws:
IllegalArgumentException - if from_Index > to_Index
ArrayIndexOutOfBoundsException - if from_Index  a.length
```

**Examples:**

We can **fill entire array**:

```java
// Java program to fill a subarray of given array
import java.util.Arrays;

public class Main
{
    public static void main(String[] args)
    {
        int ar[] = {2, 2, 1, 8, 3, 2, 2, 4, 2};

        // To fill complete array with a particular
        // value
        Arrays.fill(ar, 10);
        System.out.println("Array completely filled" +
                  " with 10\n" + Arrays.toString(ar));
    }
}
```

or **from specific index**

```java
public static void fill(Object[] a,
        int fromIndex,
        int toIndex,
        Object val)
```
