Introduction
The Arrays.fill()
method in Java is a utility method that assigns a specified value to each element of an array or a specified range within an array. This method is part of the java.util
package and is useful for initializing or resetting arrays. It supports primitive types and object arrays and offers overloaded versions for filling entire arrays or specific ranges within arrays.
Key Points:
- Value Assignment: The method assigns a specified value to each element of the array or within a specified range.
- Overloaded Methods: Supports different data types, including
boolean
,byte
,char
,double
,float
,int
,long
,short
, andObject
. - Range Specification: Allows filling a specific range within an array using inclusive-exclusive indexing.
Syntax
The Arrays.fill()
method has several overloaded versions. Here are some of the common signatures:
void Arrays.fill(boolean[] a, boolean val);
void Arrays.fill(boolean[] a, int fromIndex, int toIndex, boolean val);
void Arrays.fill(byte[] a, byte val);
void Arrays.fill(byte[] a, int fromIndex, int toIndex, byte val);
void Arrays.fill(char[] a, char val);
void Arrays.fill(char[] a, int fromIndex, int toIndex, char val);
void Arrays.fill(double[] a, double val);
void Arrays.fill(double[] a, int fromIndex, int toIndex, double val);
void Arrays.fill(float[] a, float val);
void Arrays.fill(float[] a, int fromIndex, int toIndex, float val);
void Arrays.fill(int[] a, int val);
void Arrays.fill(int[] a, int fromIndex, int toIndex, int val);
void Arrays.fill(long[] a, long val);
void Arrays.fill(long[] a, int fromIndex, int toIndex, long val);
void Arrays.fill(short[] a, short val);
void Arrays.fill(short[] a, int fromIndex, int toIndex, short val);
void Arrays.fill(Object[] a, Object val);
void Arrays.fill(Object[] a, int fromIndex, int toIndex, Object val);
- a: The array to fill.
- fromIndex, toIndex: The indices defining the range within the array to fill (inclusive-exclusive).
- val: The value to assign to each element of the array or specified range.
Example: Using Arrays.fill()
Let’s explore how to use the Arrays.fill()
method with various examples.
Example 1: Filling an Entire Array of Booleans
import java.util.Arrays;
public class FillExample {
public static void main(String[] args) {
// Array of booleans
boolean[] array = new boolean[5];
// Fill the entire array with true
Arrays.fill(array, true);
// Print the filled array
System.out.println("Boolean array filled with true: " + Arrays.toString(array));
}
}
Output:
Boolean array filled with true: [true, true, true, true, true]
Explanation:
- Entire Array: The method assigns the value
true
to each element of the boolean array.
Example 2: Filling a Range of Bytes
import java.util.Arrays;
public class FillRangeBytes {
public static void main(String[] args) {
// Array of bytes
byte[] array = {1, 2, 3, 4, 5};
// Fill a range of the array with 9
Arrays.fill(array, 1, 4, (byte) 9);
// Print the filled array
System.out.println("Byte array after filling range: " + Arrays.toString(array));
}
}
Output:
Byte array after filling range: [1, 9, 9, 9, 5]
Explanation:
- Range Fill: The method assigns the value
9
to the elements in the range from index1
to3
(inclusive-exclusive).
Example 3: Filling an Entire Array of Characters
import java.util.Arrays;
public class FillCharacters {
public static void main(String[] args) {
// Array of chars
char[] array = new char[4];
// Fill the entire array with 'X'
Arrays.fill(array, 'X');
// Print the filled array
System.out.println("Char array filled with 'X': " + Arrays.toString(array));
}
}
Output:
Char array filled with 'X': [X, X, X, X]
Explanation:
- Entire Array: The method assigns the value
'X'
to each element of the character array.
Example 4: Filling an Entire Array of Doubles
import java.util.Arrays;
public class FillDoubles {
public static void main(String[] args) {
// Array of doubles
double[] array = new double[5];
// Fill the entire array with 3.14
Arrays.fill(array, 3.14);
// Print the filled array
System.out.println("Double array filled with 3.14: " + Arrays.toString(array));
}
}
Output:
Double array filled with 3.14: [3.14, 3.14, 3.14, 3.14, 3.14]
Explanation:
- Entire Array: The method assigns the value
3.14
to each element of the double array.
Example 5: Filling a Range of Integers
import java.util.Arrays;
public class FillRangeIntegers {
public static void main(String[] args) {
// Array of integers
int[] array = {0, 1, 2, 3, 4, 5};
// Fill a range of the array with 99
Arrays.fill(array, 2, 5, 99);
// Print the filled array
System.out.println("Integer array after filling range: " + Arrays.toString(array));
}
}
Output:
Integer array after filling range: [0, 1, 99, 99, 99, 5]
Explanation:
- Range Fill: The method assigns the value
99
to the elements in the range from index2
to4
(inclusive-exclusive).
Example 6: Filling an Object Array
import java.util.Arrays;
public class FillObjects {
public static void main(String[] args) {
// Array of strings
String[] array = new String[4];
// Fill the entire array with "Hello"
Arrays.fill(array, "Hello");
// Print the filled array
System.out.println("Object array filled with 'Hello': " + Arrays.toString(array));
}
}
Output:
Object array filled with 'Hello': [Hello, Hello, Hello, Hello]
Explanation:
- Entire Array: The method assigns the value
"Hello"
to each element of the object array.
Real-World Use Case
In real-world applications, Arrays.fill()
can be used to initialize or reset arrays, fill placeholders, or prepare arrays for testing.
Example: Initializing a Grid
Consider a scenario where you need to initialize a grid with a default value before performing operations.
import java.util.Arrays;
public class GridInitialization {
public static void main(String[] args) {
// Dimensions of the grid
int rows = 3;
int cols = 3;
// Create a 2D array (grid)
int[][] grid = new int[rows][cols];
// Initialize the grid with a default value
for (int i = 0; i < rows; i++) {
Arrays.fill(grid[i], -1);
}
// Print the initialized grid
for (int[] row : grid) {
System.out.println(Arrays.toString(row));
}
}
}
Output:
[-1, -1, -1]
[-1, -1, -1]
[-1, -1, -1]
Explanation:
- Grid Initialization: The method is used to fill each row of the grid with a default value
-1
, preparing it for further operations.
Conclusion
The Arrays.fill()
method in Java provides a convenient way to initialize or reset arrays. With its overloaded methods and support for various data types, it is useful for a wide range of applications, including testing, initialization, and placeholder filling.
Summary:
- Value Assignment: Assigns a specified value to each element of an array or specified range.
- Overloaded Methods: Supports different data types and range-based fills.
- Use Cases: Suitable for initializing arrays, resetting data, and preparing test cases.
- Range Specification: Allows filling specific ranges within arrays using inclusive-exclusive indexing.
By understanding and utilizing the Arrays.fill()
method, developers can efficiently manage array initialization and value assignment in their Java applications.