Merging Two Arrays in Java

Class name: Merge
Data members:
int x: stores the number of students for the first list.
int y: stores the number of students for the second list.
String a[]: stores the names of x number of students.
String b[]: stores the names of y number of students.
String c[]: stores the merged list of students.
Methods:
Merge(int m, int n): parameterized constructor that initializes the variables x and y respectively, and creates the array objects accordingly.
void input(): to input the names of the students for both the lists.
void merge(): to merge the arrays a and b into c.
void sort(): to sort the elements of the merged list in lexicographical order, and then rearrange the elements in such a way that the first string is placed in the first block, the second string is placed in the last block, the third string in the second block, the fourth string in the second-last block, and so on.
void display(): to display the final sorted array.

Also include the main() method to create an object of the class to enable the task accordingly.

Program:

import java.io.*;
class Merge{
    int x;
    int y;
    String a[];
    String b[];
    String c[];
    public Merge(int m, int n){
        x = m;
        y = n;
        a = new String[x];
        b = new String[y];
        c = new String[x + y];
    }
    public void input()throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.println("First List:");
        for(int i = 0; i < x; i++){
            System.out.print("Name: ");
            a[i] = br.readLine();
        }
        System.out.println("\nSecond List:");
        for(int i = 0; i < y; i++){
            System.out.print("Name: ");
            b[i] = br.readLine();
        }
    }
    public void merge(){
        int i = 0;
        int j = 0;
        for(i = 0; i < x; i++)
            c[j++] = a[i];
        for(i = 0; i < y; i++)
            c[j++] = b[i];
    }
    public void sort(){
        String t[] = new String[c.length];
        int i = 0;
        int j = 0;
        int m = 0;
        for(i = 0; i < c.length; i++){
            for(j = 0; j < c.length - 1 - i; j++){ 
                if(c[j].compareToIgnoreCase(c[j + 1]) > 0){
                    String temp = c[j];
                    c[j] = c[j + 1];
                    c[j + 1] = temp;
                }
            }
        }
        i = 0;
        j = c.length - 1;
        while(i < j){
            t[i++] = c[m++];
            t[j--] = c[m++];
        }
        if(c.length % 2 == 1)
            t[i] = c[m];
        c = t;
    }
    public void display(){
        System.out.println("\nSorted Array:");
        for(int i = 0; i < c.length; i++)
            System.out.print(c[i] + "\t");
        System.out.println();
    }
    public static void main(String args[])
    throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("Size of the first list: ");
        int s1 = Integer.parseInt(br.readLine());
        System.out.print("Size of the second list: ");
        int s2 = Integer.parseInt(br.readLine());
        Merge obj = new Merge(s1, s2);
        obj.input();
        obj.merge();
        obj.sort();
        obj.display();
    }
}

Leave a Reply