Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fundamentals.arrays;
- import java.util.Arrays;
- import java.util.Scanner;
- public class Lab6EqualArrays {
- public static void main(String[] args) {
- Scanner scanner = new Scanner (System.in);
- int[] firstArray = parseArr(scanner);
- int[] secondArray = parseArr(scanner);
- boolean equals = Arrays.equals(firstArray,secondArray);
- int otherIndex = -1;
- for (int i = 0; i < firstArray.length; i++) {
- if (firstArray[i]!=secondArray[i]){
- otherIndex = i;
- break;
- }
- }
- if (equals){
- int sum = 0;
- for (int i : firstArray) {
- sum+=i;
- }
- System.out.printf("Arrays are identical. Sum: %d%n",sum);
- }else{
- System.out.printf("Arrays are not identical. Found difference at %d index.", otherIndex);
- }
- }
- private static int[] parseArr(Scanner scanner) {
- String firstInput = scanner.nextLine();
- String[] firstNumbers = firstInput.split(" ");
- int[] firstArray = new int[firstNumbers.length];
- for (int i = 0; i < firstArray.length; i++) {
- firstArray[i] = Integer.parseInt(firstNumbers[i]);
- }
- return firstArray;
- }
- }
- /*
- * Read two arrays and print on the console whether they are identical or not.
- * Arrays are identical if their elements are equal. If the arrays are identical
- * find the sum of the first one and print on the console following message: "Arrays are identical. Sum: {sum}",
- * otherwise find the first index where the arrays differ and print on the console
- * following message: "Arrays are not identical. Found difference at {index} index."*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement