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
import java.util.*;
public class Gfg {
public static void main(String args[]) throws Exception
{
int array[] = new int [ 10 ];
for ( int i = 0 ; i < array.length; i++)
{
array[i] = i + 1 ;
}
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
import java.util.*;
public class GFG
{
public static void main(String args[]) throws Exception
{
int array[] = { 1 , 2 , 3 , 4 , 5 };
for ( int i = 0 ; i < array.length; i++)
{
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
import java.util.*;
public class Gfg {
public static void main(String args[]) throws Exception {
int array[] = new int [ 10 ];
Arrays.fill(array, 10 );
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
import java.util.Arrays;
public class Gfg {
public static void main(String args[])
{
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] + " " );
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
import java.util.Arrays;
public class Gfg {
public static void main(String args[]) {
int [] array = new int [ 10 ];
Arrays.setAll(array, p -> p > 9 ? 0 : p);
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
import java.io.*;
import java.util.ArrayList;
public class ArrayListDemo {
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" );
System.out.println( "First ArrayList: "
+ list);
ArrayList sec_list = new ArrayList();
sec_list = (ArrayList)list.clone();
System.out.println( "Second ArrayList is: "
+ sec_list);
}
}
|
Output:
First ArrayList: [Geeks, for, Geeks, 10, 20]
Second ArrayList is: [Geeks, for, Geeks, 10, 20]