-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathExercise08_29.java
57 lines (51 loc) · 1.73 KB
/
Exercise08_29.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package ch_08;
import java.util.*;
/**
* 8.29 (Identical arrays) The two-dimensional arrays m1 and m2 are identical if they
* have the same contents. Write a method that returns true if m1 and m2 are identical, using the following header:
* public static boolean equals(int[][] m1, int[][] m2)
* <p>
* <p>
* Write a test program that prompts the user to enter two 3 * 3 arrays of integers
* and displays whether the two are identical. Here are the sample runs.
* Enter list1: 51 5 22 6 1 4 24 54 6
* Enter list2: 51 22 25 6 1 4 24 54 6
* The two arrays are not identical
* Enter list1: 51 25 22 6 1 4 24 54 6
* Enter list2: 51 22 25 6 1 4 24 54 6
* The two arrays are identical
*/
public class Exercise08_29 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[][] one = new int[3][3];
System.out.print("Enter list1: ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
one[i][j] = in.nextInt();
}
}
int[][] two = new int[3][3];
System.out.print("\nEnter list2: ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
two[i][j] = in.nextInt();
}
}
if (equals(one, two)) {
System.out.println("The arrays are identical.");
} else {
System.out.println("The arrays are not identical.");
}
}
public static boolean equals(int[][] m1, int[][] m2) {
for (int i = 0; i < m1.length; i++) {
for (int j = 0; j < m1[i].length; j++) {
if (m1[i][j] != m2[i][j]) {
return false;
}
}
}
return true;
}
}