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

04 Feb, 2022
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 valueDeclare them at the time of the creationUsing Arrays.fill()Using Arrays.copyOf()Using Arrays.setAll()Using ArrayUtils.clone()
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()
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.
Java
// Java program to fill the element in an arrayimport 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] + " ");        }   }}
  
1 2 3 4 5 6 7 8 9 10
In this method, we declare the elements of the array at the time of the creation itself.
 
 
Java
// Java program to fill the element in an arrayimport 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] + " ");        }   }}
  
1 2 3 4 5
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.
 
 
Java
// Java program to fill the element in an arrayimport 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));   }}
  
Array completely filled with 10
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
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.
 
 
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] + " ");    }}
  
Original Array:
1 2 3 
New array copy (of higher length):
1 2 3 0 0
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.
 
 
Java
// Java program to illustrate setAll to set valueimport java.util.Arrays; public class Gfg { // Main functionpublic 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));    }}
  
Array completely filled: 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
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.
 
 
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);    }}
First ArrayList: [Geeks, for, Geeks, 10, 20]
Second ArrayList is: [Geeks, for, Geeks, 10, 20]
sumitgumber28
Java-Array-Programs
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Constructors in Java
Stream In Java
Exceptions in Java
Different ways of Reading a text file in Java
Functional Interfaces in Java
Convert a String to Character array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?