The Wayback Machine - https://web.archive.org/web/20231023070538/https://www.geeksforgeeks.org/arrays-fill-java-examples/
Open In App
Related Articles

Arrays.fill() in Java with Examples

Improve Article
Improve
Save Article
Save
Like Article
Like

java.util.Arrays.fill() method is in java.util.Arrays class. This method assigns the specified data type value to each element of the specified range of the specified array.

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.
Exceptions it Throws:
IllegalArgumentException - if from_Index > to_Index
ArrayIndexOutOfBoundsException - if from_Index  a.length

Examples:

We can fill entire array.




// 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));
    }
}


Output:

Array completely filled with 10
[10, 10, 10, 10, 10, 10, 10, 10, 10]

We can fill part of array.




// Java program to fill a subarray array with 
// given value.
import java.util.Arrays;
  
public class Main
{
    public static void main(String[] args)
    {
        int ar[] = {2, 2, 2, 2, 2, 2, 2, 2, 2};
  
        // Fill from index 1 to index 4.
        Arrays.fill(ar, 1, 5, 10);
     
        System.out.println(Arrays.toString(ar));
    }
}


Output:

[2, 10, 10, 10, 10, 2, 2, 2, 2]

We can fill a multidimensional array
We can use a loop to fill a multidimensional array.
1)Fill 2D Array




// Java program to fill a multidimensional array with 
// given value.
import java.util.Arrays;
  
public class Main
{
    public static void main(String[] args)
    {
        int [][]ar = new int [3][4];
  
        // Fill each row with 10. 
        for (int[] row : ar)
            Arrays.fill(row, 10);
     
        System.out.println(Arrays.deepToString(ar));
    }
}


Output:

[[10, 10, 10, 10], [10, 10, 10, 10], [10, 10, 10, 10]]

2) Fill 3D Array




// Java program to fill a multidimensional array with 
// given value. 
  
import java.util.Arrays;
  
class GFG {
  
    public static void main(String[] args) {
        int[][][] ar = new int[3][4][5];
  
        // Fill each row with -1. 
        for (int[][] row : ar) {
            for (int[] rowColumn : row) {
                Arrays.fill(rowColumn, -1);
            }
        }
  
        System.out.println(Arrays.deepToString(ar));
    }
}


Output:

[[[-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1]], [[-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1]], [[-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1]]]

This article is contributed by Mohit Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 07 Dec, 2018
Like Article
Save Article
Similar Reads
Related Tutorials