Signup/Sign In

Java Arrays fill() Method

In this tutorial, we will learn about fill() method of Arrays class in Java. Using this method we can assign the particular value to all the array elements or the elements in the specified range. When it is required to initialize a specific value to the group of elements from an array at that time we use this method.

Syntax

public static void fill(datatype[] array,datatype val)

This method receives an array and a value then it will assign that value to all the array indexes.

List of Overloading Methods of fill() Method

This table contains all the overloaded variants of fill() method.

Method Description

static void fill(boolean[] array, boolean val)

This method assigns the specified boolean value to each element of the specified array of booleans.

static void fill(boolean[] array, int fromIndex, int toIndex, boolean val)

This method assigns the specified boolean value to each element of the specified range of the specified array of booleans.

static void fill(byte[] array, byte val)

This method assigns the specified byte value to each element of the specified array of bytes.

static void fill(byte[] array, int fromIndex, int toIndex, byte val)

This method assigns the specified byte value to each element of the specified range of the specified array of bytes.

static void fill(char[] array, char val)

This method assigns the specified char value to each element of the specified array of chars.

static void fill(char[] array, int fromIndex, int toIndex, char val)

This method assigns the specified char value to each element of the specified range of the specified array of chars.

static void fill(double[] array, double val)

This method assigns the specified double value to each element of the specified array of doubles.

static void fill(double[] array, int fromIndex, int toIndex, double val)

This method assigns the specified double value to each element of the specified range of the specified array of doubles.

static void fill(float[] array, float val)

This method assigns the specified float value to each element of the specified array of floats.

static void fill(float[] array, int fromIndex, int toIndex, float val)

This method assigns the specified float value to each element of the specified range of the specified array of floats.

static void fill(int[] array, int val)

This method assigns the specified int value to each element of the specified array of ints.

static void fill(int[] array, int fromIndex, int toIndex, int val)

This method assigns the specified int value to each element of the specified range of the specified array of ints.

static void fill(long[] array, int fromIndex, int toIndex, long val)

This method assigns the specified long value to each element of the specified range of the specified array of longs.

static void fill(long[] array, long val)

This method assigns the specified long value to each element of the specified array of longs.

static void fill(short[] array, int fromIndex, int toIndex, short val)

This method assigns the specified short value to each element of the specified range of the specified array of shorts.

static void fill(short[] array, short val)

This method assigns the specified short value to each element of the specified array of shorts.

static void fill(Object[] array, int fromIndex, int toIndex, Object val)

This method assigns the specified Object reference to each element of the specified range of the specified array of Objects.

static void fill(Object[] array, Object val)

This method assigns the specified Object reference to each element of the specified array of Objects.

Example: Fill Elements into Array

In the program given below, we can see an existing array arr containing different elements with different value but after applying the fill() method all the array elements filled with value 5.

import java.util.Arrays;
class StudyTonight { 
	public static void main(String args[]) 
	{ 
		int arr[] = {12, 21, 31, 81, 53, 24, 42, 41, 62}; 		  
		Arrays.fill(arr, 5); 
		for(int num:arr)
		{
			System.out.print(num+" ");
		} 
	} 
}


5 5 5 5 5 5 5 5 5

Example: Fill Elements into Array to a particular index

The above method is for initializing all the array elements. What if we want to initialize a particular range of elements? In that, we have another overloading method similar to the other overloading methods of the Arrays class. In the code given below, we can see array elements from the 2nd to 7th index are replaced with 8.

import java.util.Arrays;
class StudyTonight { 
	public static void main(String args[]) 
	{ 
		int arr[] = {12, 21, 31, 81, 53, 24, 42, 41, 62}; 		  
		Arrays.fill(arr, 2, 7, 8); 
		for(int num:arr)
		{
			System.out.print(num+" ");
		} 
	} 
}


12 21 8 8 8 8 8 41 62

Example of Its Overloaded Methods

In the example below, we implemented fill() method on various data types like float, long, and char. We can understand implementation by comparing the parameters in the code and output in the console.

import java.util.Arrays;
class StudyTonight{ 
	public static void main(String args[]) 
	{  
		//example for float
		float array1[] = {12, 21, 31, 1, 53, 24, 42, 41, 62}; 
		System.out.println("\nExample of fill() with flaot:\n"+Arrays.toString(array1));
		Arrays.fill(array1, 2, 7, 44); 
		System.out.println(Arrays.toString(array1));

		//example for long
		long array2[] = {8, 7, 5, 1, 9, 2, 4, 7, 3}; 
		System.out.println("\nExample of fill() with long:\n"+Arrays.toString(array2));
		Arrays.fill(array2, 1, 6, 9); 
		System.out.println(Arrays.toString(array2));

		//example for char
		char array3[] = {'s', 't', 'u', 'd', 'y', 't', 'o', 'n', 'i', 'g', 'h', 't'}; 
		System.out.println("\nExample of fill() with long:\n"+Arrays.toString(array3));
		Arrays.fill(array3, 0, 8, 's'); 
		System.out.println(Arrays.toString(array3));
	}
}


Example of fill() with flaot:
[12.0, 21.0, 31.0, 1.0, 53.0, 24.0, 42.0, 41.0, 62.0]
[12.0, 21.0, 44.0, 44.0, 44.0, 44.0, 44.0, 41.0, 62.0]

Example of fill() with long:
[8, 7, 5, 1, 9, 2, 4, 7, 3]
[8, 9, 9, 9, 9, 9, 4, 7, 3]

Example of fill() with long:
[s, t, u, d, y, t, o, n, i, g, h, t]
[s, s, s, s, s, s, s, s, i, g, h, t]

Conclusion

In this tutorial, we learned how fill() method initializes the complete array using public static void fill(datatype[] array,datatype val). If we want to initialize a specific range at that time we have another overloading method public static void fill(datatype[] array,datatype fromIndex, datatype toIndex, datatype val).



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.