How to Fill (initialize at once) an Array in Java?
Last Updated :
11 Jan, 2025
An array is a group of like-typed variables that are referred to by a common name. In this, article we will learn about Filling array in Java while Initialization.
Example:
Java
// Java program to fill the element in an array
import java.util.*;
public class Geeks {
public static void main(String args[]) throws Exception {
int a[] = new int[10];
// Adding elements in the array
for (int i = 0; i < a.length; i++)
a[i] = i + 1;
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
}
}
Output1 2 3 4 5 6 7 8 9 10
There are mutiple ways to fill an array in Java. They are as follows:
- Declare them at the time of the creation
- Using Arrays.fill()
- Using Arrays.copyOf()
- Using Arrays.setAll()
- Using ArrayUtils.clone()
1. Declare them at the time of the creation
In this method, we declare the elements of the array at the time of the creation itself.
Example:
Java
import java.util.*;
public class Geeks{
public static void main(String args[]) throws Exception {
// Array Declaration with elements
int a[] = { 1, 2, 3, 4, 5 };
for (int i = 0; i < array.length; i++)
System.out.print(array[i] + " ");
}
}
Output:
1 2 3 4 5
2. Using Arrays.fill() Method
java.util.Arrays.fill() method is in java.util.Arrays class. This method assigns the specified data type value to each element of the specified range of the specified array. You can learn more about from this article.
Example:
Java
import java.util.*;
public class Geeks {
public static void main(String args[]) throws Exception {
int a[] = new int[10];
// Filling the data
Arrays.fill(a, 10);
System.out.println(Arrays.toString(a));
}
}
Output[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
3. Using Arrays.copyOf() Method
java.util.Arrays.copyOf() method is in java.util.Arrays class. It copies the specified array, truncating or padding with false (if necessary) so the copy has the specified length. You can learn more about from this article.
Example:
Java
import java.util.Arrays;
public class Geeks {
public static void main(String args[]){
int[] a = new int[] { 1, 2, 3 };
// copying array org to copy
// Here, new array has 5 elements - two
// elements more than the original array
int[] arr2 = Arrays.copyOf(a, 5);
for (int i = 0; i < arr2.length; i++)
System.out.print(arr2[i] + " ");
}
}
4. Using Arrays.setAll() Method
It set all the element in the specified array in by the function which compute each element. You can learn more about from this article.
Example:
Java
// Java program to illustrate setAll to set value
import java.util.Arrays;
public class Gfg {
public static void main(String args[]){
int[] array = new int[10];
// Setting the value in the array
Arrays.setAll(array, p -> p > 9 ? 0 : p);
System.out.println(Arrays.toString(array));
}
}
Output[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
5. Using ArrayUtils.clone()
The Java.util.ArrayList.clone() method is used to create a shallow copy of the mentioned array list. It just creates a copy of the list. You can learn more about from this article.
Example:
Java
// Java code to illustrate clone() method
import java.io.*;
import java.util.ArrayList;
public class Geeks {
public static void main(String args[]){
ArrayList<String> list = new ArrayList<String>();
list.add("Geeks");
list.add("for");
list.add("Geeks");
list.add("10");
list.add("20");
// Creating another linked list and copying
ArrayList sec_list = new ArrayList();
sec_list = (ArrayList)list.clone();
System.out.println(sec_list);
}
}
Output[Geeks, for, Geeks, 10, 20]
Similar Reads
How to Extend an Array After Initialisation in Java? In java, the arrays are immutable i.e. if the array is once assigned or instantiated the memory allocated for the array can't be decreased or increased. But there is one form of a solution in which we can extend the array. In this article, we will learn to increase the size of Array.Extending an Arr
2 min read
How to Take Array Input From User in Java? Arrays in Java are an important data structure, and we can add elements to them by taking input from the user. There is no direct method to take input from the user, but we can use the Scanner Class or the BufferedReader class, or the InputStreamReader Class.Different Ways to Take Array Input from U
6 min read
How to Add an Element at Particular Index in Java ArrayList? ArrayList.add() method is used to add an element at particular index in Java ArrayList. Syntax: public void add(int index, Object element) ; Parameters: index -position at which the element has to be inserted. The index is zero-based.element - the element to be inserted at the specified position. Ex
2 min read
How to Initialize HashSet Values Using Constructor in Java? In Java programming, HashSet is a collection framework that is implemented by the HashSet class. It only contains unique elements and does not allow duplicate values. Mainly it does not maintain any insertion order but is inserted based on hash code. We can initialize HashSet values in many ways in
2 min read
How to Declare an ArrayList with Values in Java? ArrayList is simply known as a resizable array. Declaring an ArrayList with values is a basic task that involves the initialization of a list with specific elements. It is said to be dynamic as the size of it can be changed. Proceeding with the declaration of ArrayList, we have to be aware of the co
2 min read
How to Initialize a Vector with a Specific Initial Capacity in Java? In Java, Vector Class allows to creation of dynamic arrays that can grow or shrink as per the need. If we know the approximate size of elements, we will store this in the vector. Then we optimize memory usage, and we can initialize it with the specific initial capacity. In this article, we will lear
2 min read