Write a program to create 2 single dimension arrays in order to store 5 numbers and 6 numbers respectively. Then merge those values of both the array into third array so that the values of the first array will be kept first from left to right followed by values of second array again from left to right. Then display those values of the merged(third) array.
Use only java.util.* package
The output of the program must be like below:
Enter 5 numbers for first array
34 57 879 21 9
Enter 6 numbers for second array
12 3 4567 23 31 98
Values of first array from left to right followed by second array again from left to right
34 57 879 21 9
12 3 4567 23 31 98
Values of merged array
34 57 879 21 9 12 3 4567 23 31 98
import java.util.*;
public class App {
/**
* The start point of the program
*
* @param args
*
*/
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
System.out.println("Enter 5 numbers for first array: ");
int numbers1[] = new int[5];
for (int i = 0; i < 5; i++) {
numbers1[i] = keyBoard.nextInt();
}
System.out.println("Enter 6 numbers for second array: ");
int numbers2[] = new int[6];
for (int i = 0; i < 6; i++) {
numbers2[i] = keyBoard.nextInt();
}
int[] numbers3 = new int[numbers1.length + numbers2.length];
System.arraycopy(numbers1, 0, numbers3, 0, numbers1.length);
System.arraycopy(numbers2, 0, numbers3, numbers1.length, numbers2.length);
System.out.println("Values of merged array");
for (int i = 0; i < numbers3.length; i++) {
System.out.print(numbers3[i] + " ");
}
keyBoard.close();
}
}
Comments
Leave a comment