Arrays.fill() in Java with examples

In this tutorial, we will understand Arrays.fill() in Java. Fill is a method of Arrays class in Java. Now let’s understand these terms.

Understanding Arrays.Fill() in Java using different examples for the same.

Firstly, Arrays is a pre-defined class in Java in its Util Package. This class contains various methods which are used to manipulate the data in Arrays data structure, for instance: sort() function sorts the array given to it, toString() function converts the data in the array to a string, similarly, equals() function compares two arrays of similar data types, etc.

Talking about fill() member function, this function has two syntax, basically, this function assigns a given value to each element in the array. Given are the two syntax for the function.

Image : (source: Java Document, Oracle)

Arrays.fill() in Java with examples

Firstly, it requires an array and a value, then it assigns that value to every element in that array.

Secondly, it requires an array, an integer value of fromIndex, an integer value of toIndex and value, then it assigns that value to every element from fromIndex to toIndex indices in that array.

This function does not return any value. The only exceptions it throws is firstly, IllegalArgumentException – if fromIndex is greater than toIndex and secondly, ArrayIndexOutOfBoundsException – if fromIndex < 0 or fromIndex > array.length.

Here in this tutorial, we are going to apply fill() function on 1-D, 2-D, and 3-D arrays in Java. Let us understand using codes for each one of them.

1.a : For 1-D array: Calling fill(int array[] , int value)

import java.util.Arrays; 
  
public class FillExample
{ 
    public static void main(String[] args) 
    { 
    int array[] = {1,2,3,4,5,6,7,8,9,0}; 
    System.out.println("Array before calling fill method: " + Arrays.toString(array));
  
    // Fill from index 1 to index 6. 
    Arrays.fill(array, 10); 
 
    System.out.println("Array after calling fill method: " + Arrays.toString(array)); 
    } 
}

Output :

Array before calling fill method: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
Array after calling fill method: [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]

1.b : For 1-D array: Calling fill(int array[], int fromIndex, int toIndex, int value)

import java.util.Arrays; 
  
public class FillExample
{ 
    public static void main(String[] args) 
    { 
        int array[] = {1,2,3,4,5,6,7,8,9,0}; 
        System.out.println("Array before calling fill method: " + Arrays.toString(array));
  
        // Fill from index 1 to index 6. 
        Arrays.fill(array, 1, 7, 10); 
     
        System.out.println("Array after calling fill method: " + Arrays.toString(array)); 
    } 
}

Output :

Array before calling fill method: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
Array after calling fill method: [1, 10, 10, 10, 10, 10, 10, 8, 9, 0]

2 : For 2-D array : Calling fill(int array[], int value)

import java.util.Arrays; 
  
public class FillExample
{ 
  public static void main(String[] args) 
    { 
        int [][]array = new int [3][4]; 
  
        for (int[] row : array) 
            Arrays.fill(row, 10); 
     
        System.out.println(Arrays.deepToString(array)); 
    } 
}

Output:

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

3 : For 3-D array: Calling fill(int array[], int value)

import java.util.Arrays; 

public class FillExample 
{ 
    public static void main(String[] args) 
     { 
         int [][][]array = new int [3][3][3]; 
         for (int[][] row : array)
         { 
            for (int[] column : row) 
            { 
                Arrays.fill(column, 10); 
            } 
        } 
         System.out.println(Arrays.deepToString(array));
         
     }
     
    
}

Output :

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

Thank You, I hope the function of Fill is clearly explained to you.

You can also visit:  Get common elements from two Arrays in JavaScript

One response to “Arrays.fill() in Java with examples”

  1. Mohit Kumar says:

    Was looking for this everywhere and found it at Codespeedy.
    Thanks, OP Ekta Sharma you did great work 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *