Note : Save this class inside ArrayMerger.java file under java project in Eclipse IDE
import java.util.Scanner;
public class ArrayMerger
{
public static void main(String[] args)
{
// create object of scanner to get data from keyboard
Scanner s = new Scanner(System.in);
System.out.print("Enter size for firstArray:");
int size1 = s.nextInt();
// create an array of given size
int[] firstArray = new int[size1];
// loop to store data inside array using for loop
for (int i = 0; i < firstArray.length; i++)
{
System.out.print("Input value for index "+i+" of firstArray : ");
// get a value from keyboard and store it inside array
firstArray[i] = s.nextInt();
}
// show a message
System.out.println("** firstArray initialized **");
System.out.print("Enter size for secondArray:");
int size2 = s.nextInt();
// create an array of given size
int[] secondArray = new int[size2];
// loop to store data inside secondArray using for loop
for (int i = 0; i < secondArray.length; i++)
{
System.out.print("Input value for index "+i+" of secondArray : ");
// get a value from keyboard and store it inside array
secondArray[i] = s.nextInt();
}
// show a message
System.out.println("** secondArray initialized **");
// show a menu
System.out.println("## Menu ##");
System.out.println("Type 1 to merge secondArray after firstArray");
System.out.print("Type 2 to merge firstArray after secondArray =>");
// get a value from keyboard and store it as a choice
int choice = s.nextInt();
// create a new array, its size is equal to the size of firstArray plus size of secondArray
int[] thirdArray = new int[firstArray.length + secondArray.length];
// check choice
if(choice == 1)
{
// this variable will be used as an index for thisrArray
int index = 0;
// fetch data from firstArray and store it inside thirdArray
for (int i = 0; i < firstArray.length; i++)
{
thirdArray[index] = firstArray[i];
// increase the index of thirdArray
index++;
}
// fetch data from secondArray and store it inside thirdArray
for (int i = 0; i < secondArray.length; i++)
{
thirdArray[index] = secondArray[i];
// increase the index of thirdArray
index++;
}
}
else
if(choice == 2)
{
// this variable will be used as an index for thirdArray
int index = 0;
// fetch data from firstArray and store it inside thirdArray
for (int i = 0; i < secondArray.length; i++)
{
thirdArray[index] = secondArray[i];
// increase the index of thirdArray
index++;
}
// fetch data from secondArray and store it inside thirdArray
for (int i = 0; i < firstArray.length; i++)
{
thirdArray[index] = firstArray[i];
// increase the index of thirdArray
index++;
}
}
else
{
System.out.println("invalid choice");
}
// show thirdArray using for loop
for (int i = 0; i < thirdArray.length; i++)
{
System.out.println("index :"+i+", element :"+thirdArray[i]);
}
}
}
import java.util.Scanner;
public class ArrayMerger
{
public static void main(String[] args)
{
// create object of scanner to get data from keyboard
Scanner s = new Scanner(System.in);
System.out.print("Enter size for firstArray:");
int size1 = s.nextInt();
// create an array of given size
int[] firstArray = new int[size1];
// loop to store data inside array using for loop
for (int i = 0; i < firstArray.length; i++)
{
System.out.print("Input value for index "+i+" of firstArray : ");
// get a value from keyboard and store it inside array
firstArray[i] = s.nextInt();
}
// show a message
System.out.println("** firstArray initialized **");
System.out.print("Enter size for secondArray:");
int size2 = s.nextInt();
// create an array of given size
int[] secondArray = new int[size2];
// loop to store data inside secondArray using for loop
for (int i = 0; i < secondArray.length; i++)
{
System.out.print("Input value for index "+i+" of secondArray : ");
// get a value from keyboard and store it inside array
secondArray[i] = s.nextInt();
}
// show a message
System.out.println("** secondArray initialized **");
// show a menu
System.out.println("## Menu ##");
System.out.println("Type 1 to merge secondArray after firstArray");
System.out.print("Type 2 to merge firstArray after secondArray =>");
// get a value from keyboard and store it as a choice
int choice = s.nextInt();
// create a new array, its size is equal to the size of firstArray plus size of secondArray
int[] thirdArray = new int[firstArray.length + secondArray.length];
// check choice
if(choice == 1)
{
// this variable will be used as an index for thisrArray
int index = 0;
// fetch data from firstArray and store it inside thirdArray
for (int i = 0; i < firstArray.length; i++)
{
thirdArray[index] = firstArray[i];
// increase the index of thirdArray
index++;
}
// fetch data from secondArray and store it inside thirdArray
for (int i = 0; i < secondArray.length; i++)
{
thirdArray[index] = secondArray[i];
// increase the index of thirdArray
index++;
}
}
else
if(choice == 2)
{
// this variable will be used as an index for thirdArray
int index = 0;
// fetch data from firstArray and store it inside thirdArray
for (int i = 0; i < secondArray.length; i++)
{
thirdArray[index] = secondArray[i];
// increase the index of thirdArray
index++;
}
// fetch data from secondArray and store it inside thirdArray
for (int i = 0; i < firstArray.length; i++)
{
thirdArray[index] = firstArray[i];
// increase the index of thirdArray
index++;
}
}
else
{
System.out.println("invalid choice");
}
// show thirdArray using for loop
for (int i = 0; i < thirdArray.length; i++)
{
System.out.println("index :"+i+", element :"+thirdArray[i]);
}
}
}