An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives (int, char, etc.) as well as the object (or non-primitive) references of a class depending on the definition of the array. In the case of primitive data types, the actual values are stored in contiguous memory locations. In the case of objects of a class, the actual objects are stored in the heap segment. There are six ways to fill an array in Java. They are as follows:
- Using for loop to fill the value
- Declare them at the time of the creation
- Using Arrays.fill()
- Using Arrays.copyOf()
- Using Arrays.setAll()
- Using ArrayUtils.clone()
Method 1: Using for loop to fill the value
In this method, we run the empty array through the loop and place the value at each position. This is mostly used in programming as it helps the coder to place the desired value at each position.
Example:
Java
// Java program to fill the element in an array import java.util.*; public class Gfg { // Main function public static void main(String args[]) throws Exception { // Array Declaration int array[] = new int [ 10 ]; // Adding elements in the array for ( int i = 0 ; i < array.length; i++) { array[i] = i + 1 ; } // Printing the elements for ( int i = 0 ; i < array.length; i++) { System.out.print(array[i] + " " ); } } } |
Output:
1 2 3 4 5 6 7 8 9 10
Method 2: 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
// Java program to fill the element in an array import java.util.*; public class GFG { // Main function public static void main(String args[]) throws Exception { // Array Declaration with elements int array[] = { 1 , 2 , 3 , 4 , 5 }; // Printing the elements for ( int i = 0 ; i < array.length; i++) { // Printing Elements System.out.print(array[i] + " " ); } } } |
Output:
1 2 3 4 5
Method 3: Using Arrays.fill()
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
// Java program to fill the element in an array import java.util.*; public class Gfg { // Main function public static void main(String args[]) throws Exception { // Empty Array Declaration int array[] = new int [ 10 ]; // Filling the data Arrays.fill(array, 10 ); // Printing the data System.out.println( "Array completely filled with 10\n" + Arrays.toString(array)); } } |
Output:
Array completely filled with 10 [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
Method 4: Using Arrays.copyOf()
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
// Java program to illustrate copyOf when new array // is of higher length. import java.util.Arrays; public class Gfg { public static void main(String args[]) { // initializing an array original int [] org = new int [] { 1 , 2 , 3 }; System.out.println( "Original Array : \n" ); for ( int i = 0 ; i < org.length; i++) System.out.print(org[i] + " " ); // copying array org to copy // Here, new array has 5 elements - two // elements more than the original array int [] copy = Arrays.copyOf(org, 5 ); System.out.print( "\New array copy (of higher length):\n" ); for ( int i = 0 ; i < copy.length; i++) System.out.print(copy[i] + " " ); } } |
Output:
Original Array: 1 2 3 New array copy (of higher length): 1 2 3 0 0
5: Using Arrays.setAll()
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 { // Main function public static void main(String args[]) { // initializing an array int [] array = new int [ 10 ]; // Setting the value in the array Arrays.setAll(array, p -> p > 9 ? 0 : p); // Printing the array System.out.println( "Array completely filled: \n" + Arrays.toString(array)); } } |
Output:
Array completely filled: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Method 6: 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 ArrayListDemo { public static void main(String args[]) { // Creating an empty ArrayList ArrayList<String> list = new ArrayList<String>(); // Use add() method // to add elements in the list list.add( "Geeks" ); list.add( "for" ); list.add( "Geeks" ); list.add( "10" ); list.add( "20" ); // Displaying the list System.out.println( "First ArrayList: " + list); // Creating another linked list and copying ArrayList sec_list = new ArrayList(); sec_list = (ArrayList)list.clone(); // Displaying the other linked list System.out.println( "Second ArrayList is: " + sec_list); } } |
Output:
First ArrayList: [Geeks, for, Geeks, 10, 20] Second ArrayList is: [Geeks, for, Geeks, 10, 20]