The Arrays.fill() method in Java is a utility function provided by the java.util.Arrays class. It is used to assign a specified value to each element of an array. This method is particularly useful for initializing arrays or resetting their values efficiently.
Usage
The Arrays.fill() method is available for different data types, including int, char, double, boolean, and more. It simplifies the process of populating arrays with a default or specific value.
Syntax
Arrays.fill(array, value);
Arrays.fill(array, fromIndex, toIndex, value);Didukung oleh
array: The array to be filled.
value: The value to assign to each element.
fromIndex: The starting index (inclusive) for filling.
toIndex: The ending index (exclusive) for filling.
Examples
Example 1: Fill Entire Array
import java.util.Arrays;publicclassFillArrayExample{publicstaticvoidmain(String[] args){
int[] numbers =newint[5];
Arrays.fill(numbers,10);
System.out.println(Arrays.toString(numbers));}}Didukung oleh
In this example, the Arrays.fill() method is used to fill the entire numbers array with the value 10. The Arrays.toString() method is then used to print the array, resulting in [10, 10, 10, 10, 10].
Example 2: Fill Partial Array
import java.util.Arrays;publicclassPartialFillArrayExample{publicstaticvoidmain(String[] args){
char[] chars ={'a','b','c','d','e'};
Arrays.fill(chars,1,4,'x');
System.out.println(Arrays.toString(chars));}}Didukung oleh
Here, the Arrays.fill() method fills a portion of the chars array from index 1 to 3 (inclusive of 1, exclusive of 4) with the character 'x'. The resulting array is [a, x, x, x, e].
Example 3: Fill Boolean Array
import java.util.Arrays;publicclassBooleanFillArrayExample{publicstaticvoidmain(String[] args){
boolean[] flags =newboolean[4];
Arrays.fill(flags,true);
System.out.println(Arrays.toString(flags));}}Didukung oleh
This example demonstrates filling a boolean array flags with the value true. The Arrays.fill() method sets each element of the array to true, resulting in [true, true, true, true].
Tips and Best Practices
Efficiency: Use Arrays.fill() for efficient initialization of arrays with a constant value, reducing the need for manual loops.
Index Bounds: Ensure that the fromIndex and toIndex are within the array's bounds to avoid ArrayIndexOutOfBoundsException.
Data Types: The method is overloaded to support various data types, so make sure to use the appropriate method for your array type.
Partial Filling: Utilize the partial filling capability to modify specific segments of an array without affecting other elements.
Default Initialization: Use Arrays.fill() to quickly reset an array to a default state, especially in algorithms requiring multiple iterations with fresh data.
The
Arrays.fill()method in Java is a utility function provided by thejava.util.Arraysclass. It is used to assign a specified value to each element of an array. This method is particularly useful for initializing arrays or resetting their values efficiently.Usage
The
Arrays.fill()method is available for different data types, includingint,char,double,boolean, and more. It simplifies the process of populating arrays with a default or specific value.Syntax
array: The array to be filled.value: The value to assign to each element.fromIndex: The starting index (inclusive) for filling.toIndex: The ending index (exclusive) for filling.Examples
Example 1: Fill Entire Array
In this example, the
Arrays.fill()method is used to fill the entirenumbersarray with the value10. TheArrays.toString()method is then used to print the array, resulting in[10, 10, 10, 10, 10].Example 2: Fill Partial Array
Here, the
Arrays.fill()method fills a portion of thecharsarray from index 1 to 3 (inclusive of 1, exclusive of 4) with the character'x'. The resulting array is[a, x, x, x, e].Example 3: Fill Boolean Array
This example demonstrates filling a boolean array
flagswith the valuetrue. TheArrays.fill()method sets each element of the array totrue, resulting in[true, true, true, true].Tips and Best Practices
Arrays.fill()for efficient initialization of arrays with a constant value, reducing the need for manual loops.fromIndexandtoIndexare within the array's bounds to avoidArrayIndexOutOfBoundsException.Arrays.fill()to quickly reset an array to a default state, especially in algorithms requiring multiple iterations with fresh data.