Open In App

How to Fill (initialize at once) an Array in Java?

Last Updated : 11 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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] + " ");
   }
}

Output
1 2 3 4 5 6 7 8 9 10 

 There are mutiple ways to fill an array in Java. They are as follows:

  1. Declare them at the time of the creation
  2. Using Arrays.fill()
  3. Using Arrays.copyOf()
  4. Using Arrays.setAll()
  5. 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] + " ");
    }
}

Output
1 2 3 0 0 

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]



Next Article
Practice Tags :

Similar Reads