below you find with the inner class ArrayComparator a simplified custom comparator for Object arrays, which works as you will see when running the code.
Now I would like to use this comparator in method Arrays.compare(T[] a, T[] b, Comparator<? super T> cmp) , and modified the comparator for this purpose in ArrayComparator2 . The code as given below compiles nicely, but if you remove the comment slashes of the three lines in the main method, the compiler finds no suitable method for compare . How to fix this?
import java.awt.*;
import java.util.*;
public class CompareTest {
static Object[] o1= {new Point(1,2), new Point(3,4), new Point(5,6)};
static Object[] o2= {new Point(1,2), new Point(3,4), new Point(5,6)};
public static void main(String args[]) {
if (args.length>0) {
if (args[0].equals("1"))
((Point)o2[2]).y= 7;
else
((Point)o1[2]).y= 7;
}
for (int i=0; i<o1.length; i++) {
System.out.println(o1[i]+", "+o2[i]);
}
ArrayComparator comparator= new ArrayComparator();
int result= comparator.compare(o1, o2);
System.out.println("Result: "+result);
// ArrayComparator2<Object[]> comparator2= new ArrayComparator2<>();
// result= Arrays.compare(o1, o2, comparator2);
// System.out.println("Result: "+result);
System.exit(0);
}
static class ArrayComparator implements Comparator<Object[]> {
public int compare(Object[] o1, Object[] o2) {
int l= o1.length;
if (l!=o2.length) {
return l>o2.length ? 1 : -1;
}
for (int i=0; i<l; i++) {
int j= (o1[i].toString()).compareTo(o2[i].toString());
if (j!=0) return j;
}
return 0;
}
}
static class ArrayComparator2<T> implements Comparator<T[]> {
public int compare(T[] o1, T[] o2) {
int l= o1.length;
if (l!=o2.length) {
return l>o2.length ? 1 : -1;
}
for (int i=0; i<l; i++) {
int j= (o1[i].toString()).compareTo(o2[i].toString());
if (j!=0) return j;
}
return 0;
}
}
/* No solution either:
static class ArrayComparator2<Object> implements Comparator<Object[]> {
public int compare(Object[] o1, Object[] o2) {
int l= o1.length;
if (l!=o2.length) {
return l>o2.length ? 1 : -1;
}
for (int i=0; i<l; i++) {
int j= (o1[i].toString()).compareTo(o2[i].toString());
if (j!=0) return j;
}
return 0;
}
}
*/
}
From the method signature of Arrays.compare
, it can be seen that the Comparator
is used for comparing the array elements, not the arrays themselves. In other words, you should provide a Comparator<T>
, not a Comparator<T[]>
.
static class ArrayComparator2<T> implements Comparator<T> {
public int compare(T o1, T o2) {
return o1.toString().compareTo(o2.toString());
}
}
// ...
ArrayComparator2<Object> comparator2= new ArrayComparator2<>();
result= Arrays.compare(o1, o2, comparator2);
System.out.println("Result: "+result);
The Arrays.compare()
method does take the arrays as input with a custom comparator, but it compares the values of the arrays (upto the length of the smaller array) with the comparator you've provided. Just look at the implementation:
public static <T> int compare(T[] a, T[] b, Comparator<? super T> cmp) {
Objects.requireNonNull(cmp);
if (a == b) {
return 0;
} else if (a != null && b != null) {
int length = Math.min(a.length, b.length);
for(int i = 0; i < length; ++i) {
T oa = a[i];
T ob = b[i];
if (oa != ob) {
int v = cmp.compare(oa, ob);
if (v != 0) {
return v;
}
}
}
return a.length - b.length;
} else {
return a == null ? -1 : 1;
}
}
So, either you should change the class signature to:
static class ArrayComparator2<T> implements Comparator<T> {
public int compare(T o1, T o2) {
return o1.toString().compareTo(o2.toString());
}
}
And instantiate the comparator class as:
ArrayComparator2<Object> comparator2= new ArrayComparator2<>();
OR
JUST USE THE customComparator.compare() for your desired array element check and other stuffs.
ArrayComparator2.compare(o1,o2);
There's no way around.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.