Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, arrays are used to store multiple values of the same type in a single variable. In this tutorial, we'll discuss various ways to fill an array in Java.
int[] numbers = {1, 2, 3, 4, 5}; String[] names = {"Alice", "Bob", "Charlie"};
int[] numbers = new int[5]; for (int i = 0; i < numbers.length; i++) { numbers[i] = i + 1; } for (int number : numbers) { System.out.println(number); }
Arrays.fill()
method is part of the java.util package and can be used to fill an array with a specified value:import java.util.Arrays; public class ArrayFillingExample { public static void main(String[] args) { int[] numbers = new int[5]; Arrays.fill(numbers, 42); // Fill the array with the value 42 for (int number : numbers) { System.out.println(number); } } }
int[][] matrix = new int[2][2]; // Using nested loops for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { matrix[i][j] = i + j; } } // Initializing at declaration int[][] anotherMatrix = { {1, 2}, {3, 4} };
IntStream.range()
method, followed by the toArray()
method:import java.util.stream.IntStream; public class ArrayFillingExample { public static void main(String[] args) { int[] numbers = IntStream.range(1, 6).toArray(); for (int number : numbers) { System.out.println(number); } } }
In this example, we create an IntStream with a range from 1 (inclusive) to 6 (exclusive) and convert it to an array using the toArray()
method.
In conclusion, there are various ways to fill an array in Java, such as initializing the array at declaration, using a loop, the Arrays.fill()
method, or the Java 8 Stream API. Choose the method that best suits your needs and programming style.
Arrays.fill() method in Java
The Arrays.fill()
method allows you to fill an array with a specific value.
int[] array = new int[5]; Arrays.fill(array, 42);
Filling arrays with default values in Java
Java automatically initializes arrays with default values (0 for numeric types, false
for boolean, null
for objects).
int[] array = new int[5]; // Array is automatically filled with default values (0 in this case)
Custom array filling in Java
You can fill arrays with custom logic using loops or other mechanisms.
int[] array = new int[5]; for (int i = 0; i < array.length; i++) { array[i] = i * 10; }
Efficient ways to fill arrays in Java
For large arrays, using Arrays.fill()
is often more efficient than manual looping.
int[] array = new int[1000000]; Arrays.fill(array, 42);
Initializing and populating arrays in Java
Use array initialization syntax to populate values directly.
int[] array = {1, 2, 3, 4, 5};
Java fill array with random values
You can fill an array with random values using Random
class.
int[] array = new int[5]; Random random = new Random(); for (int i = 0; i < array.length; i++) { array[i] = random.nextInt(100); // Random values between 0 and 99 }
Filling arrays with a specific value in Java
Use Arrays.fill()
to fill an array with a specific value.
double[] array = new double[5]; Arrays.fill(array, 3.14);
Bulk array filling techniques in Java
Java provides various bulk array filling techniques, like using Arrays.fill()
or initializing arrays directly.
int[] array = new int[5]; // Using Arrays.fill() Arrays.fill(array, 42); // Initializing directly int[] newArray = {1, 2, 3, 4, 5};