Java Program to Copy an Array

Write a Java Program to Copy an Array without using Built-in function and using a copyof method with an example.

Java Program to Copy an Array using CopyOf Method

In this program, we are using the copyOf method to copy one array to another.

import java.util.Scanner;
import java.util.Arrays;

public class CpyArr1 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Size, i;
		sc = new Scanner(System.in);
	 
		System.out.print(" Please Enter Number of Items : ");
		Size = sc.nextInt();	
		
		int [] a = new int[Size];
		
		System.out.print(" Please Enter " + Size + " elements : ");
		for (i = 0; i < Size; i++)
		{
			a[i] = sc.nextInt();
		}   
		
		int[] b = Arrays.copyOf(a, Size);
		
		System.out.println("\n **** Elements in b[] after Duplicating are **** ");
		for (i = 0; i < Size; i++)
		{
			System.out.println(" Element at b["+ i +"] = " + b[i]);
		}
	}
}
 Please Enter Number of Items : 5
 Please Enter 5 elements  : 10 20 35 64 70

 **** Elements in b[] after Duplicating are **** 
 Element at b[0] = 10
 Element at b[1] = 20
 Element at b[2] = 35
 Element at b[3] = 64
 Element at b[4] = 70

Java Program to Copy an Array using For Loop

This Java program allows the user to enter the size and the One Dimensional Array elements. Next, it copies each element in a[] to b[] array using For Loop.

import java.util.Scanner;

public class CopyArray2 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Size, i;
		sc = new Scanner(System.in);
	 
		System.out.print(" Please Enter Number of elements in an array : ");
		Size = sc.nextInt();	
		
		int [] a = new int[Size];
		int [] b = new int[Size];
		
		System.out.print(" Please Enter " + Size + " elements of an Array  : ");
		for (i = 0; i < Size; i++)
		{
			a[i] = sc.nextInt();
		}   
		
		for (i = 0; i < Size; i++)
		{
			b[i] = a[i];
		} 
		
		System.out.println("\n **** Elements in b[] Array after Copying are **** ");
		for (i = 0; i < Size; i++)
		{
			System.out.println(" Element at b["+ i +"] = " + b[i]);
		}
	}
}
Java Program to Copy an Array 2

First, We declared two one-dimensionals of user-specified size. And they are a, b of integer type.

The below For Loop iterate each cell present in the []. The condition inside the for loops (i < Size)) ensures that the compiler does not exceed the size. Statement inside the for loop stores the user entered values as array element such as a[0], a[1], a[2], a[3], a[4].

for (i = 0; i < Size; i++)
{
	a[i] = sc.nextInt();
}

In the next line, we have one more for loop to iterate every element, and save it in b[]

for (i = 0; i < Size; i++)
{
	b[i] = a[i];
}

In this Java Program to Copy an Array example, User inserted values are a[5] = {19, 25, 18, 79, 125}}

First Iteration: for (i = 0; 0 < 5; 0++)

The value of i = 0, and the condition (i < 5) is True. So, it starts executing the statements inside the loop until the condition fails.

b[i] = a[i]
b[0] = a[0]
b[0] = 19

Second Iteration: for (i = 1; 1 < 5; 1++)
The value of i is 1, and the condition (i < 5) is True.

b[1] = a[1]
b[1] = 25

Third Iteration: for (i = 2; 2 < 5; 2++)

b[2] = a[2]
b[2] = 18

Do the same for remaining iterations until the condition (i < 5) fails.

The next for loop is to print the elements in b[]. I suggest you refer Java Program to Print Array Elements article to understand the same

Java Program to Copy an Array using While Loop

This array duplication example is the same as above. But we used While Loop to copy elements from one array to another.

import java.util.Scanner;

public class CpyArr3 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Size, i = 0, j = 0, k = 0;
		sc = new Scanner(System.in);
	 
		System.out.print(" Please Enter Number of elements : ");
		Size = sc.nextInt();	
		
		int [] a = new int[Size];
		int [] b = new int[Size];
		
		System.out.print(" Please Enter " + Size + " elements  : ");
		while(i < Size)
		{
			a[i] = sc.nextInt();
			i++;
		}   
		
		while(j < Size)
		{
			b[j] = a[j];
			j++;
		} 
		
		System.out.println("\n **** Elements in b[] after Duplicating are **** ");
		while(k < Size)
		{
			System.out.println(" Element at b["+ k +"] = " + b[k]);
			k++;
		}
	}
}
 Please Enter Number of elements : 7
 Please Enter 7 elements  : 42 59 86 95 87 158 268

 **** Elements in b[] after Duplicating are **** 
 Element at b[0] = 42
 Element at b[1] = 59
 Element at b[2] = 86
 Element at b[3] = 95
 Element at b[4] = 87
 Element at b[5] = 158
 Element at b[6] = 268