Arrays.fill()
is a static method in Java’s java.util.Arrays
class that assigns a specified value to each element of an array. This method is particularly useful for initializing or resetting the contents of an array to a specific value.
Arrays.fill()
Arrays.fill()
is overloaded to handle different types of arrays, including primitive types (e.g., int
, char
, double
) and object types.Here are the main overloaded variants of the Arrays.fill()
method:
public static void fill(int[] a, int val)
public static void fill(long[] a, long val)
public static void fill(float[] a, float val)
public static void fill(double[] a, double val)
public static void fill(char[] a, char val)
public static void fill(byte[] a, byte val)
public static void fill(boolean[] a, boolean val)
public static void fill(short[] a, short val)
public static void fill(Object[] a, Object val)
public static void fill(int[] a, int fromIndex, int toIndex, int val)
public static void fill(long[] a, int fromIndex, int toIndex, long val)
public static void fill(float[] a, int fromIndex, int toIndex, float val)
public static void fill(double[] a, int fromIndex, int toIndex, double val)
public static void fill(char[] a, int fromIndex, int toIndex, char val)
public static void fill(byte[] a, int fromIndex, int toIndex, byte val)
public static void fill(boolean[] a, int fromIndex, int toIndex, boolean val)
public static void fill(short[] a, int fromIndex, int toIndex, short val)
public static void fill(Object[] a, int fromIndex, int toIndex, Object val)
import java.util.Arrays;
public class ArrayFillExample {
public static void main(String[] args) {
int[] intArray = new int[5];
double[] doubleArray = new double[5];
char[] charArray = new char[5];
// Filling the entire array with a specified value
Arrays.fill(intArray, 42);
Arrays.fill(doubleArray, 3.14);
Arrays.fill(charArray, 'A');
// Printing filled arrays
System.out.println("intArray: " + Arrays.toString(intArray)); // Output: [42, 42, 42, 42, 42]
System.out.println("doubleArray: " + Arrays.toString(doubleArray)); // Output: [3.14, 3.14, 3.14, 3.14, 3.14]
System.out.println("charArray: " + Arrays.toString(charArray)); // Output: [A, A, A, A, A]
}
}
import java.util.Arrays;
public class ArrayFillExample {
public static void main(String[] args) {
String[] stringArray = new String[5];
// Filling the entire array with a specified value
Arrays.fill(stringArray, "hello");
// Printing filled array
System.out.println("stringArray: " + Arrays.toString(stringArray)); // Output: [hello, hello, hello, hello, hello]
}
}
import java.util.Arrays;
public class ArrayFillExample {
public static void main(String[] args) {
int[] intArray = new int[10];
// Filling a subrange of the array with a specified value
Arrays.fill(intArray, 2, 7, 99);
// Printing the array after filling
System.out.println("intArray: " + Arrays.toString(intArray)); // Output: [0, 0, 99, 99, 99, 99, 99, 0, 0, 0]
}
}
fromIndex
is greater than the toIndex
, an IllegalArgumentException
is thrown. If either fromIndex
or toIndex
are out of range (less than 0 or greater than the array length), an ArrayIndexOutOfBoundsException
is thrown.Arrays.fill()
is generally efficient and can be used to quickly initialize or reset large arrays.In summary, Arrays.fill()
is a versatile and efficient method for initializing or resetting arrays in Java. It provides various overloaded versions to handle different data types and offers flexibility by allowing you to fill entire arrays or specific subranges.