TheBulgarianWolf

Are The 2 Arrays Equal?

Apr 26th, 2020
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3. public class Main
  4. {
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.         System.out.print("Enter the first array: ");
  8.         String arrayOne = sc.nextLine();
  9.         String[] arrOne = arrayOne.split(" ");
  10.         int[] arr1 = Arrays.stream(arrOne).mapToInt(e -> Integer.parseInt(e)).toArray();
  11.         System.out.print("Enter the second array: ");
  12.         String arrayTwo = sc.nextLine();
  13.         String[] arrTwo = arrayTwo.split(" ");
  14.         int[] arr2 = Arrays.stream(arrTwo).mapToInt(e -> Integer.parseInt(e)).toArray();
  15.         int sum = 0;
  16.         boolean isTrue = true;
  17.         if(arr1.length == arr2.length){
  18.             for(int i = 0;i<arr1.length;i++){
  19.                 if(arr1[i] != arr2[i]){
  20.                     System.out.println("Arrays are not identical.Found difference at index: " + i);
  21.                     isTrue = false;
  22.                     break;
  23.                 }
  24.                     sum += arr1[i];
  25.             }
  26.         }
  27.        
  28.         if(isTrue == true){
  29.             System.out.println("Arrays are identical.Sum: " + sum);
  30.         }
  31.     }
  32. }
Add Comment
Please, Sign In to add comment