Kasper123

Untitled

Sep 9th, 2025
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class TwoDimensionalArrayTest {
  4.  
  5.     public static boolean equals(int[][] a, int[][] b) {
  6.         for (int i = 0; i < a.length; i++) {
  7.             for (int j = 0; j < a[i].length; j++) {
  8.                 if (a[i][j] != b[i][j]) return false;
  9.             }
  10.         }
  11.         return true;
  12.     }
  13.  
  14.     public static void main(String[] args) {
  15.         Scanner input = new Scanner(System.in);
  16.  
  17.         System.out.print("How many elements (the size) in the first array ==> ");
  18.         int rows1 = input.nextInt();
  19.         int cols1 = input.nextInt();
  20.         input.nextLine();
  21.         int[][] array1 = new int[rows1][cols1];
  22.         System.out.println("Enter the elements of the first array ==> ");
  23.         for (int i = 0; i < rows1; i++) {
  24.             String[] parts = input.nextLine().trim().split("\\s+");
  25.             for (int j = 0; j < cols1; j++) {
  26.                 array1[i][j] = Integer.parseInt(parts[j]);
  27.             }
  28.         }
  29.  
  30.         System.out.print("How many elements (the size) in the second array ==> ");
  31.         int rows2 = input.nextInt();
  32.         int cols2 = input.nextInt();
  33.         input.nextLine();
  34.         int[][] array2 = new int[rows2][cols2];
  35.         System.out.println("Enter the elements of the second array ==> ");
  36.         for (int i = 0; i < rows2; i++) {
  37.             String[] parts = input.nextLine().trim().split("\\s+");
  38.             for (int j = 0; j < cols2; j++) {
  39.                 array2[i][j] = Integer.parseInt(parts[j]);
  40.             }
  41.         }
  42.  
  43.         if (rows1 != rows2 || cols1 != cols2) {
  44.             System.out.println("The size of the two arrays are not equals");
  45.         } else {
  46.             if (equals(array1, array2)) {
  47.                 System.out.println("The two arrays are strictly identical arrays");
  48.             } else {
  49.                 System.out.println("The two arrays are not strictly identical arrays");
  50.             }
  51.         }
  52.  
  53.         input.close();
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment