Showing posts with label compare two string array without comparing each element i.e. compare all together.. Show all posts
Showing posts with label compare two string array without comparing each element i.e. compare all together.. Show all posts

Sunday, May 25, 2014

compare two string array without comparing each element i.e. compare all together.

Note- Even if order of the string values different in the arrays then also it will say arrays are not same. Both the arrays should have same elements in same order then it will say, both the array are same.

import java.util.Scanner;

public class CompareArray {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter size of string array: ");
        int size = in.nextInt();
        String[] array1 = new String[size];
        String[] array2 = new String[size];
        System.out.println("enter the element of 1st array: ");
        for(int i=0; i<size; i++){
            array1[i] = in.next();
        }
        System.out.println("enter the element of 2nd array: ");
        for(int i=0; i<size; i++){
            array2[i] = in.next();
        }
       
        String s1="", s2 ="";
        for(int i=0; i<size; i++){
            s1 = s1+array1[i];
            s2 = s2+array2[i];
        }
        if(s1.equals(s2)){
            System.out.println("both the array have same elements.");
            System.out.println(s1);
            System.out.println(s2);
        }else{
            System.out.println("both the array does not have the same elements.");
            System.out.println(s1);
            System.out.println(s2);
        }
    }
}