Thursday, February 23, 2012

Comparing Arrays in java



import java.io.*;
import java.util.*;

public class ComparingArrays{
  public static void main(String[] args) throws IOException{
    int[] array1 = new int[5];
    int[] array2 = new int[5];
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    try{
      System.out.println("Enter 5 numbers for the first Array : ");
      for(int i = 0; i < array1.length; i++){
        array1[i] = Integer.parseInt(in.readLine());
      }
      System.out.println("Enter 5 numbers for the second Array : ");
      for(int i = 0; i < array2.length; i++){
        array2[i] = Integer.parseInt(in.readLine());
      }
    }
    catch(NumberFormatException ne){
      ne.printStackTrace();
    }

    boolean check = Arrays.equals(array1, array2);
    if(check == false)
      System.out.println("Arrays are not same.");
    else
      System.out.println("Both Arrays are same.");
  }
}


No comments:

Post a Comment