Java Array Fill

The Java Array Fill Method is one of the Array Methods, which is to assign user-specified value to every element in the specified range (if specified) of an array.

In this article, we will show how to fill the Boolean array, Byte Array, Object Array, Integer Array, Char Array, Long Array, Double Array, Float Array, Short Array and Object array. The basic syntax of the Arrays.fill in Java Programming language is as shown below.

Java Array Fill Method syntax

The Java Programming Language provides eighteen different methods to fill the Java Array. The following Java Array Fill method will accept the Byte Array and byte value as the parameter and assigns the byte value to every element in a Byte array. Alternatively, we can say, fill method will replace all the array elements with user-specified byte value.

public static void fill(byte[] anByteArray, byte Value); 

//In order to use in program
Arrays.fill(byte[] anByteArray, byte Value);

It accepts the Byte Array as the first argument, starting index position (fromIndex), where the filling will begin as the second parameter (integer value), last index position (toIndex), where the filling will end as the third argument, and byte value (array element replaced by this value).

NOTE: Java Arrays.fill method will fill the array elements starting from the fromIndex up to toIndex but not included.

public static void fill(byte[] anByteArray, int fromIndex, int toIndex, byte Value); 

//In order to use in program
Arrays.fill(byte[] anByteArray, int fromIndex, int toIndex, byte Value);

This Java Array Fill method will accept the Boolean Array and Boolean value as the parameter and assigns the Boolean value to every element in a Boolean array.

public static void fill(boolean[] anBooleanArray, boolean Value); 

//In order to use in program
Arrays.fill(boolean[] anBooleanArray, boolean Value);

The following java fill method will accept the Boolean Array as the first argument, starting index position (fromIndex) as the second parameter (integer value), last index position (toIndex) as the third argument, and the Boolean value.

public static void fill(boolean[] anBooleanArray, int fromIndex, int toIndex, boolean Value); 

//In order to use in program
Arrays.fill(boolean[] anBooleanArray, int fromIndex, int toIndex, boolean Value);

This Java Array Fill method accepts the short Array and short value as the parameter. It assigns the short value to every element in a short array.

public static void fill(short[] anShortArray, short Value); 

//In order to use in program
Arrays.fill(short[] anShortArray, short Value);

It accepts the Short Array as the first argument, starting index position (fromIndex, last index position (toIndex), and Short value (array element replaced by this value).

public static void fill(short[] anShortArray, int fromIndex, int toIndex, short Value); 

//In order to use in program
Arrays.fill(short[] anShortArray, int fromIndex, int toIndex, short Value);

This Java Fill method accepts the Character Array and Char value as the parameter. It assigns the Character value to every element in a Character array.

public static void fill(char[] anCharArray, char Value); 

//In order to use in program
Arrays.fill(char[] anCharArray, char Value);

The following java array fill method accepts the Character Array, starting index position (fromIndex), last index position (toIndex), and Character value to replace array element.

public static void fill(char[] anCharArray, int fromIndex, int toIndex, char Value); 

//In order to use in program
Arrays.fill(char[] anCharArray, int fromIndex, int toIndex, char Value);

It accepts the Integer Array and Integer value as the parameter. It assigns the Integer value to every element in an Integer array.

public static void fill(int[] anIntArray, int Value); 

//In order to use in program
Arrays.fill(int[] anIntArray, int Value);

The Java Fill method accepts the Integer Array as the first argument, starting index position (fromIndex), last index position (toIndex, and Integer value (array element replaced by this value).

public static void fill(int[] anIntArray, int fromIndex, int toIndex, int Value); 

//In order to use in program
Arrays.fill(int[] anIntArray, int fromIndex, int toIndex, int Value);

It accepts the Long Array and Long value as the parameter. It assigns the Long value to every element in a Long array.

public static void fill(long[] anLongArray, long Value); 

//In order to use in program
Arrays.fill(long[] anLongArray, long Value);

The following method accepts the Long Array as the first argument, starting index position (fromIndex), last index position (toIndex), and Long value (array element replaced by this value).

public static void fill(long[] anLongArray, int fromIndex, int toIndex, long Value);

//In order to use in program
Arrays.fill(long[] anLongArray, int fromIndex, int toIndex, long Value);

This Java Array Fill method will accept the Double Array and Double value as the parameter and assigns the Double value to every element in a Double array.

public static void fill(double[] anDoubleArray, double Value);

//In order to use in program
Arrays.fill(double[] anDoubleArray, double Value);

It accepts the Double Array as the first argument, starting index position (fromIndex) where the filling will begin as the second parameter (integer value), last index position (toIndex) where the filling will end as the third argument, and Double value (array element replaced by this value).

public static void fill(double[] anDoubleArray, int fromIndex, int toIndex, double Value);

//In order to use in program
Arrays.fill(double[] anDoubleArray, int fromIndex, int toIndex, double Value);

The following method will accept the Floating-point Array and Float value as the parameter and assigns the Floating-point value to every element in a Float array.

public static void fill(float[] anFloatArray, float Value); 

//In order to use in program
Arrays.fill(float[] anFloatArray, float Value);

This Java Arrays.Fill method accepts the Floating-point Array, starting index position (fromIndex), last index position (toIndex), and Floating-point value.

public static void fill(float[] anFloatArray, int fromIndex, int toIndex, float Value); 

//In order to use in program
Arrays.fill(float[] anFloatArray, int fromIndex, int toIndex, float Value);

It accepts the Object Array and Object value as the parameter and assigns the Object value to every element in an Object array.

public static void fill(Object[] Array, Object Val); 

//In order to use in program
Arrays.fill(Object[] Array, Object Val);

The following method will accept the Object Array, starting index position (fromIndex), last index position (toIndex), and Object value (array element replaced by this value).

public static void fill(Object[] Array, int fromIndex, int toIndex, Object Val); 

//In order to use in program
Arrays.fill(Object[] Array, int fromIndex, int toIndex, Object Val);
  • fromIndex: Please specify the starting index position. It is the index position where the Filling will begin.
  • toIndex: Please specify the end index position. Java Arrays.fill method will fill up to this index position. However, it will not include the element at this position (toIndex).
  • Value: Please specify the value that you want to assign for every element of the Java Array.

Fill Byte Array using Java Array Fill Method

In this Java program, we declared the byte array with random array elements. Then we will fill the Java array elements with a new value.

// Filling Byte Array using Java Arrays.fill Method
package ArrayMethods;
import java.util.Arrays;
public class ByteFill {
	public static void main(String[] args) {
		byte[] byteArray = {20, 8, 18, 45}; 
		byte[] bitArray = {10, 25, 8, 19, 16}; 
		
		//Filling Array with Value 5
		Arrays.fill(byteArray, (byte)5);	
		System.out.println("Filling Byte Array:");
		arrayPrint(byteArray);
		
		//Filling Array with Value 7 using Index
		Arrays.fill(bitArray, 1, 4, (byte)7);		
		System.out.println("Filling Byte Array Using Index:");
		arrayPrint(bitArray);
	}
	public static void arrayPrint(byte[] anByteArray) {
		for (byte Number: anByteArray) {
			System.out.println("Array Elelment = " + Number);
		}
	}
}
Java Array Fill 1

The following statement will call the public static void fill(byte[] anByteArray, byte Value) method to assign the Value to every element in a byte array.

Arrays.fill(byteArray, (byte)5);

The following Java statement is to print the Byte array elements to the output.

arrayPrint(byteArray);

When the compiler reaches the above statement, the compiler will jump to the following function. From the below code snippet, we used the Java Foreach Loop to iterate the Byte Array. Then we are printing every array element using the System.out.println statement.

public static void arrayPrint(byte[] anByteArray) {
	for (byte Number: anByteArray) {
		System.out.println("Array Elelment = " + Number);
	}
}

The following statement calls public static void fill(byte[] anByteArray, int fromIndex, int toIndex, byte Value) to fill the byte array from index position 1 to position 3 with value 7.

Arrays.fill(bitArray, 1, 4, (byte)7);

Fill Boolean Array using Java Array Fill Method

In this Java program, we declared the Boolean array with random array elements. Then we will fill the array elements with a new value.

// Filling Boolean Array using Java Arrays.fill Method
package ArrayMethods;
import java.util.Arrays;

public class BooleanFill {
	public static void main(String[] args) {
		boolean[] bool = {true, false, false, true};
		boolean[] boolArray = {true, false, true, true, true};
		
		Arrays.fill(bool, true);
		System.out.println("Boolean Array:");
		arrayPrint(bool);
		
		Arrays.fill(boolArray, 1, 4, false);
		System.out.println("Filling Boolean Array Using Index:");
		arrayPrint(boolArray);
	}
	public static void arrayPrint(boolean[] anBooleanArray) {
		for (boolean bool: anBooleanArray) {
			System.out.println("Array Elelment = " + bool);
		}
	}
}
Boolean Array:
Array Elelment = true
Array Elelment = true
Array Elelment = true
Array Elelment = true
Filling Boolean Array Using Index:
Array Elelment = true
Array Elelment = false
Array Elelment = false
Array Elelment = false
Array Elelment = true

The following statement will call the public static void fill(boolean[] anBooleanArray, Boolean Value) method to assign the Value to every element in a Boolean array.

Arrays.fill(bool, true);

It calls the public static void fill(boolean[] anBooleanArray, int fromIndex, int toIndex, Boolean Value) method to fill the Boolean array from index position 1 to position 3 with Boolean False.

Arrays.fill(boolArray, 1, 4, false);

Fill Short Array using Java Array Fill Method

In this Java program, we declared the Short array with random array elements. Then we will Sort the Java short array elements in Ascending Order.

// Filling Short Array using Java Arrays.fill Method
package ArrayMethods;
import java.util.Arrays;

public class ShortFill {
	public static void main(String[] args) {
		short[] shrArray = {12, 14, 2, 9}; 
		short[] ShortArray = {25, 5, 19, 6, 50}; 		

		Arrays.fill(shrArray, (short)15);
		System.out.println("Filling Short Array:");
		arrayPrint(shrArray);
		
		Arrays.fill(ShortArray, 1, 4,(short)75);
		System.out.println("Filling Short Array Using Index:");
		arrayPrint(ShortArray);
	}
	public static void arrayPrint(short[] anShortArray) {
		for (short Number: anShortArray) {
			System.out.println("Array Elelment = " + Number);
		}
	}
}
Filling Short Array:
Array Elelment = 15
Array Elelment = 15
Array Elelment = 15
Array Elelment = 15
Filling Short Array Using Index:
Array Elelment = 25
Array Elelment = 75
Array Elelment = 75
Array Elelment = 75
Array Elelment = 50

It calls the public static void fill(short[] anShortArray, short Value) method to assign the Value to every element in a Short array.

Arrays.fill(shrArray, (short)15);

It calls public static void fill(short[] anShortArray, int fromIndex, int toIndex, short Value) to fill the Short array from index position 1 to position 3 with 75.

Arrays.fill(ShortArray, 1, 4,(short)75);

Fill Integer Array using Java Array Fill Method

In this Java program, we declared the Integer array with random array elements. Then we will fill the Java Int array elements with a new value.

// Filling Interger Array using Java Arrays.fill Method
package ArrayMethods;
import java.util.Arrays;

public class IntFill {
	public static void main(String[] args) {
		int[] IntArray = {98, 54, 75, 82}; 
		int[] IntegerArray = {14, 25, 78, 19, 94}; 
		
		Arrays.fill(IntArray, 30);
		System.out.println("Integer Array:");
		arrayPrint(IntArray);
		
		Arrays.fill(IntegerArray, 1, 4, 50);
		System.out.println("Filling Integer Array Using Index:");
		arrayPrint(IntegerArray);
	}
	public static void arrayPrint(int[] anIntArray) {
		for (int Number: anIntArray) {
			System.out.println("Array Element = " + Number);
		}
	}
}
Integer Array:
Array Element = 30
Array Element = 30
Array Element = 30
Array Element = 30
Filling Integer Array Using Index:
Array Element = 14
Array Element = 50
Array Element = 50
Array Element = 50
Array Element = 94

It calls the public static void fill(int[] anIntegerArray, int Value) method to assign the Value to every element in an Integer array.

Arrays.fill(IntArray, 30);

It calls the public static void fill(int[] anIntegerArray, int fromIndex, int toIndex, int Value) method to fill the integer array from index position 1 to position 3 with 50.

Arrays.fill(IntegerArray, 1, 4, 50);

Fill Long Array using Java Array Fill Method

In this Java program, we declared the Long array with random array elements. Then we will fill the Java Long array elements with new values.

// Filling Long Array using Java Arrays.sort Method
package ArrayMethods;
import java.util.Arrays;

public class LongFill {
	public static void main(String[] args) {
		long[] LngArray = {198, 146, 187, 153}; 
		long[] LongArray = {1515, 250, 190, 80, 2525}; 		

		Arrays.fill(LngArray, 750);
		System.out.println("Filling Long Array:");
		arrayPrint(LngArray);
		
		Arrays.fill(LongArray, 1, 4, 1500);
		System.out.println("Filling Long Array Using Index:");
		arrayPrint(LongArray);
	}
	public static void arrayPrint(long[] anLongArray) {
		for (long Number: anLongArray) {
			System.out.println("Array Element = " + Number);
		}
	}
}
Filling Long Array:
Array Element = 750
Array Element = 750
Array Element = 750
Array Element = 750
Filling Long Array Using Index:
Array Element = 1515
Array Element = 1500
Array Element = 1500
Array Element = 1500
Array Element = 2525

In this Java Array Fill method example, the below statement calls the public static void fill(long[] anLongArray, long Value) to assign the Value to every Long array element.

Arrays.fill(LngArray, 750);

The following statement calls public static void fill(long[] anLongArray, int fromIndex, int toIndex, long Value) to fill the Long array from index position 1 to position 3 with value 1500.

Arrays.fill(LongArray, 1, 4, 1500);

Fill Double Array using Java Array Fill Method

In this Java program, we declared the Double array with random array elements. Then we will fill the Java Double array elements with a new value.

// Filling Double Array using Java Arrays.fill Method
package ArrayMethods;
import java.util.Arrays;

public class DoubleFill {
	public static void main(String[] args) {
		double[] DoubArray = {64.542, 7.98, 78.45, 35.76}; 
		double[] DoubleArray = {145.25, 98.86, 27.9865, 83.485, 209.356}; 
		
		Arrays.fill(DoubArray, 1400.25);
		System.out.println("Double Array:");
		arrayPrint(DoubArray);
		
		Arrays.fill(DoubleArray, 1, 4, 180.75);
		System.out.println("Filling Double Array Using Index:");
		arrayPrint(DoubleArray);
	}
	public static void arrayPrint(double[] anDoubleArray) {
		for (double Number: anDoubleArray) {
			System.out.println("Array Element = " + Number);
		}
	}
}
Double Array:
Array Element = 1400.25
Array Element = 1400.25
Array Element = 1400.25
Array Element = 1400.25
Filling Double Array Using Index:
Array Element = 145.25
Array Element = 180.75
Array Element = 180.75
Array Element = 180.75
Array Element = 209.356

It calls the public static void fill(double[] anDoubleArray, double Value) method to assign the Value to every element in a double array.

Arrays.fill(DoubArray, 1400.25);

The following statement calls public static void fill(double[] anDoubleArray, int fromIndex, int toIndex, double Value) to sort the Double array from index position 1 to position 3 with 180.75.

Arrays.fill(DoubleArray, 1, 4, 180.75);

Fill Float Array using Java Array Fill Method

In this Fill Java program, we declared the Float array with random array elements. Next, we will fill the Java Float array elements with new value.

// Filling Float Array using Java Arrays.fill Method
package ArrayMethods;
import java.util.Arrays;

public class FloatFill {
	public static void main(String[] args) {
		float[] floArray = {8.68f, 1.98f, 5.54f, 12.45f};
		float[] floatArray = {8.4f, 10.25f, 25.98f, 16.48f, 13.3f};	
		//Filling Array with Value 10.25
		Arrays.fill(floArray, 10.25f);
		System.out.println("Float Array:");
		arrayPrint(floArray);
		
		//Filling Array with Value 25.75 using Index
		Arrays.fill(floatArray, 1, 4, 25.75f);
		System.out.println("Filling Float Array Using Index:");
		arrayPrint(floatArray);
	}
	public static void arrayPrint(float[] anfloatArray) {
		for (float Number: anfloatArray) {
			System.out.println("Array Element = " + Number);
		}
	}
}
Float Array:
Array Element = 10.25
Array Element = 10.25
Array Element = 10.25
Array Element = 10.25
Filling Float Array Using Index:
Array Element = 8.4
Array Element = 25.75
Array Element = 25.75
Array Element = 25.75
Array Element = 13.3

The following statement will call the public static void fill(float[] anFloatArray, float Value) method to assign the Value to every element in a floating-point array.

Arrays.fill(floArray, 10.25f);

It calls the public static void fill(float[] anFloatArray, int fromIndex, int toIndex, float Value) method to fill the Float array from index position 1 to position 3 with 25.75f.

Arrays.fill(floatArray, 1, 4, 25.75f);

Fill Char Array using Java Array Fill Method

In this Java array fill program, we declared the Character array with random array elements. Then we will fill the Char array elements with a new value.

// Filling Char Array using Java Arrays.fill Method
package ArrayMethods;
import java.util.Arrays;

public class CharFill {
	public static void main(String[] args) {
		char[] CharArray = {'g', 'a', 't', 'e'}; 
		char[] CharacterArray = {'g', 'a', 't', 'e', 'y'};
		//Filling Array with Char T
		Arrays.fill(CharArray, 'T');
		System.out.println("Character Array:");
		arrayPrint(CharArray);
		
		//Filling Array with Character A using Index
		Arrays.fill(CharacterArray, 2, 4, 'A');
		System.out.println("Filling Character Array Using Index:");
		arrayPrint(CharacterArray);
	}
	public static void arrayPrint(char[] anCharacterArray) {
		for (char ch: anCharacterArray) {
			System.out.println("Array Element = " + ch);
		}
	}
}
Character Array:
Array Element = T
Array Element = T
Array Element = T
Array Element = T
Filling Character Array Using Index:
Array Element = g
Array Element = a
Array Element = A
Array Element = A
Array Element = y

In this Java Fill method example, the following statement will call the public static void fill(char [] anCharArray, char Value) method to assign the Value to each and every element in a char array.

Arrays.fill(CharArray, 'T');

The below statement will call the public static void fill(char[] anCharArray, int fromIndex, int toIndex, char Value) to fill the Char array from index position 2 to position 3 with the character ‘A’.

Arrays.fill(CharacterArray, 2, 4, 'A');

Fill Object Array using Java Array Fill Method

In this Java program, we declared the String array with random array elements. Next, we will fill the Java string object array elements with new object value.

// Filling Object Array using Java Arrays.fill Method
package ArrayMethods;
import java.util.Arrays;

public class ObjectFill {
	public static void main(String[] args) {
		String[] strArray = {"Apple", "Orange", "Kiwi"};
		String[] stringArray = {"India", "UK", "Australia", "Japan","China"};

		Arrays.fill(strArray, "Cherry");
		System.out.println("Filling Object Array:");
		arrayPrint(strArray);
		
		Arrays.fill(stringArray, 1, 4, "USA");
		System.out.println("Filling Object Array Using Index:");
		arrayPrint(stringArray);
	}
	public static void arrayPrint(Object[] anObjectArray) {
		for (Object Number: anObjectArray) {
			System.out.println("Array Element = " + Number);
		}
	}
}
Filling Object Array:
Array Element = Cherry
Array Element = Cherry
Array Element = Cherry
Filling Object Array Using Index:
Array Element = India
Array Element = USA
Array Element = USA
Array Element = USA
Array Element = China

Within this Array Fill Method example, the following statement calls public static void fill(Object[] anObjectArray, Object Value) to assign the Value to every Object array element.

Arrays.fill(strArray, "Cherry");

It calls public static void fill(Object[] anObjectArray, int fromIndex, int toIndex, Object Value). And the method fills the Object array from index position 1 to position 3 with string “USA”.

Arrays.fill(stringArray, 1, 4, "USA");