Java:从非静态比较器对其外部类字段的引用
我需要使用非静态比较器对对象列表进行排序,该比较器使用外部对象字段中的值。
class A {
public int x;
public int y;
public int z;
public Comparator<A> scoreComparator = new Comparator<A>() {
public compare(A o1, A o2) {
// System.out.println("this: " + this);
return (int) (x * o1.x - x * o2.x);
}
}
public A(int _x, int _y, int _z) {
x = _x;
y = _y;
z = _z;
}
}
A var_1 = new A(1, 2, 3);
A var_2 = new A(5, 6, 7);
List<A> list = getMyListFromSomewhere();
// the following will produce different ordering
Collections.sort(list, var_1.scoreComparator);
Collections.sort(list, var_2.scoreComparator);
但由于某种原因,这不能正常工作。当我取消注释比较器中的 println 行时,它显示引用是对 A 对象的,但它们在一次 sort() 调用中不同,因此“x”的值不同。我在这里做错了什么?
I need to sort lists of objects with a non-static comparator that uses a value from it's outer object field.
class A {
public int x;
public int y;
public int z;
public Comparator<A> scoreComparator = new Comparator<A>() {
public compare(A o1, A o2) {
// System.out.println("this: " + this);
return (int) (x * o1.x - x * o2.x);
}
}
public A(int _x, int _y, int _z) {
x = _x;
y = _y;
z = _z;
}
}
A var_1 = new A(1, 2, 3);
A var_2 = new A(5, 6, 7);
List<A> list = getMyListFromSomewhere();
// the following will produce different ordering
Collections.sort(list, var_1.scoreComparator);
Collections.sort(list, var_2.scoreComparator);
But for some reason this does not work properly. When I uncomment the println line in the comparator, it shows that the references are to A objects, but they are different within one sort() call, therefore the value of "x" is different. What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你能解释一下为什么你需要
Comparator
是非静态的吗?为什么不只是以下内容?Can you explain why do you need the
Comparator
to be non-static? Why not just the following?这取决于您想要实现的目标。上面的代码不起作用,因为您在创建
A
实例时使用了不同的x
值。每次创建
A
的实例时,您还会创建一个与A
实例绑定的比较器实例。这意味着compare()
方法中的x
是o1.x
或o2.x
。我建议创建一个新类来实现比较器,并有一个字段 x 使其独立于 A:
That depends on what you want to achieve. The code above doesn't work because you use different values of
x
when you create yourA
instances.Each time, you create an instance of
A
, you also create an instance of the comparator which is tied to the instance ofA
. That means thex
in thecompare()
method is eithero1.x
oro2.x
.I suggest to create a new class that implements the comparator and which has a field
x
to make it independent ofA
:首先让我们看看
scoreComparator
做了什么。该行也可以写为
这意味着
x
的符号 - 正或负反转比较结果将恢复排序列表中的排序。如果
x
和o1.x
或x
和的值添加到
太大了。同样,int
的转换,以确保整数溢出>o2.xx
符号只会恢复排序。由于
var_1
和var_2
字段x
的值为正,我们可以得出结论,第二种情况会导致整数溢出和不同的排序。var_1.x
等于1
,var_2.x
等于5
,这使得整数溢出的可能性增加了五倍在后一种情况下。Let's see first what the
scoreComparator
does. The linecould be also written as
which means the the sign of
x
-- positive or negative inverts comparation result will revert the ordering in the sorting list.Casting to
int
is added to ensure integer overflow if values ofx
ando1.x
orx
ando2.x
are too big. Again, the sign ofx
will just revert the ordering.Since both
var_1
andvar_2
have positive values for the fieldx
we can conclude that the second scenario causes integer overflow and different ordering.var_1.x
equals to1
, andvar_2.x
equals to5
which makes integer overflowing five times more probable in the latter case.我不是 100% 确定你想通过这个设计实现什么,但这是一个非常糟糕的设计。 如果您想要在同一类类型中使用非静态比较器,请尝试使用compareTo而不是compare。否则,按照@Aaron的建议将比较方法放在一个单独的类中。
I'm not 100% sure what you want to achieve by this design, but this is a very bad design. If you want a non-static comparator inside the same class type, try going for compareTo rather than compare. Otherwise, put the compare method in a separate class as @Aaron suggested.