Kaynağa Gözat

saddle-points: make MatrixCoordinate implement Comparable

The saddle-points tests make it seem like you can use any kind of set
for your output. However, if you use TreeSet then the tests will fail
because MatrixCoordinate doesn't implement Comparable.

MatrixCoordinate does now implement Comparable to preent this failure.
Frida Tveit 9 yıl önce
ebeveyn
işleme
c9782c11ba

+ 7
- 1
exercises/saddle-points/.meta/src/reference/java/MatrixCoordinate.java Dosyayı Görüntüle

@@ -1,4 +1,4 @@
1
-class MatrixCoordinate {
1
+class MatrixCoordinate implements Comparable<MatrixCoordinate> {
2 2
     private final int row;
3 3
     private final int col;
4 4
 
@@ -30,4 +30,10 @@ class MatrixCoordinate {
30 30
         result = 31 * result + col;
31 31
         return result;
32 32
     }
33
+
34
+    @Override
35
+    public int compareTo(MatrixCoordinate o) {
36
+        int rowComparison = Integer.compare(row, o.row);
37
+        return (rowComparison == 0) ? Integer.compare(col, o.col) : rowComparison;
38
+    }
33 39
 }

+ 7
- 1
exercises/saddle-points/src/main/java/MatrixCoordinate.java Dosyayı Görüntüle

@@ -1,4 +1,4 @@
1
-class MatrixCoordinate {
1
+class MatrixCoordinate implements Comparable<MatrixCoordinate> {
2 2
     private final int row;
3 3
     private final int col;
4 4
 
@@ -30,4 +30,10 @@ class MatrixCoordinate {
30 30
         result = 31 * result + col;
31 31
         return result;
32 32
     }
33
+
34
+    @Override
35
+    public int compareTo(MatrixCoordinate o) {
36
+        int rowComparison = Integer.compare(row, o.row);
37
+        return (rowComparison == 0) ? Integer.compare(col, o.col) : rowComparison;
38
+    }
33 39
 }