• Source
    1. /* package whatever; // don't place package name! */
    2.  
    3. import java.util.*;
    4. import java.lang.*;
    5. import java.io.*;
    6.  
    7.  
    8.  
    9. /*(Identical arrays) The two-dimensional arrays m1 and m2 are identical if they have the same contents.
    10. Write a method that returns true if m1 and m2 are identical, using the following header:
    11.  
    12. public static boolean equals(int[][] m1, int[][] m2)
    13.  
    14. Write a test program that prompts the user to enter two 3 X 3 arrays of integers
    15. and displays whether the two are identical. Here are the sample runs.
    16.  
    17. Enter list1: 51 25 22 6 1 4 24 54 6
    18. Enter list2: 51 22 25 6 1 4 24 54 6
    19. The two arrays are identical
    20.  
    21. Enter list1: 51 5 22 6 1 4 24 54 6
    22. Enter list2: 51 22 25 6 1 4 24 54 6
    23. The two arrays are not identical*/
    24. /* Name of the class has to be "Main" only if the class is public. */
    25. class Ideone
    26. {
    27. public static void main (String[] args) throws java.lang.Exception
    28. {
    29. Scanner input = new Scanner(System.in);
    30.  
    31. //Create 2 - 3 x 3 Matrices - list1 & list2
    32. int list1[][] = new int[3][3];
    33. int list2[][] = new int[3][3];
    34.  
    35. //Allow user to enter values for list1
    36. System.out.print("Enter List 1: ");
    37.  
    38. for(int x = 0; x < list1.length; x++)
    39. {
    40. for(int y = 0; y < list1[0].length; y++)
    41. {
    42. list1[x][y] = input.nextInt();
    43. }
    44. }
    45.  
    46. //Allow user to enter values for list2
    47. System.out.print("Enter List 2: ");
    48.  
    49. for(int y = 0; y < list2.length; y++)
    50. {
    51. for(int z = 0; z < list2[0].length; z++)
    52. {
    53. list2[y][z] = input.nextInt();
    54. }
    55. }
    56.  
    57. //Close the scanner object
    58. input.close();
    59.  
    60. //Use the create equals method to tell user
    61. //if the lists are identical or not
    62. if(equals(list1, list2))
    63. {
    64. System.out.println("The two arrays are identical");
    65. }
    66.  
    67. else
    68. {
    69. System.out.println("The two arrays are not identical");
    70. }
    71.  
    72. }
    73.  
    74.  
    75. //This method is going check if two arrays are equal
    76. //Method header provided by professor
    77. public static boolean equals(int[][] m1, int[][] m2){
    78.  
    79. //Check if the user entered in a 3 x 3 Matrix
    80. //Matrix should only have 9 elements
    81. if(m1.length * m1[0].length != 9 && m2.length * m2[0].length != 9)
    82. {
    83. return false;
    84. }
    85.  
    86. //Translate the M1 Matrix into a single array NewM1
    87. //This will allow me to use the sort method
    88. int NewM1[] = new int[3*3]; //new array will have 9 elements
    89.  
    90. int x = 0;
    91.  
    92. for(int i = 0; i < 3; i++)
    93. {
    94. for(int j = 0; j < 3; j++)
    95. {
    96. NewM1[x] = m1[i][j];//Consolidate the matrix's elements into the single array
    97. x++;
    98. }
    99. }
    100.  
    101. //Sort the new single array NewM1 so that I can compare the elements with NewM2
    102. java.util.Arrays.sort(NewM1);
    103.  
    104.  
    105. //Translate the matrix into a new single array NewM2
    106. //This will allow me to use the sort method
    107. int NewM2[] = new int[3*3];//New Array will have 9 elements
    108.  
    109. int c = 0;
    110.  
    111. for(int a = 0; a < 3; a++)
    112. {
    113. for(int b = 0; b < 3; b++)
    114. {
    115. NewM2[c] = m2[a][b];//Consolidate the matrix's elements into the single array
    116. c++;
    117. }
    118. }
    119.  
    120. //Sort the new single array NewM2 so that we can compare the elements with NewM1
    121. java.util.Arrays.sort(NewM2);
    122. //****************************
    123.  
    124.  
    125. //Use Arrays.equals method to see if the elements
    126. //from the sorted single arrays NewM1 & NewM2 are strictly equal
    127. if(java.util.Arrays.equals(NewM1, NewM2))
    128. {
    129. return true;
    130. }
    131.  
    132. //If they are not strictly equal after sorting the elements in order
    133. //The return false because they do not contain the same values
    134. else
    135. {
    136. return false;
    137. }
    138.  
    139. }
    140. }