Joined: Tue Mar 27, 2007 10:55 pm Posts: 2279 Location: Earth Has thanked: 39 time Have thanks: 61 time
Code:
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."); } }
you can see that the given program initializes two arrays and input five number from user through the keyboard. And then the program checks whether the given taken both arrays are same or not. This comparison operation is performed by using the equals() method of Arrays clas
_________________ Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )