为什么我不能使用Comparator对基元进行排序? [英] Why I can't use Comparator to sort primitives?
问题描述
由于Java 5具有自动装箱功能,为什么我不能使用Comparator对原语进行排序? int
不会被包装到整数
?
As Java 5 have autoboxing, why I can't use Comparator to sort primitives? An int
wouldn't be wrapped into a Integer
?
推荐答案
Arrays.sort(..)
具有用于排序原始数组的专用重载。
Arrays.sort(..)
have dedicated overloadings for sorting primitive arrays.
如果您需要除标准排序规则之外的任何特殊排序规则,我担心您必须使用自动装箱。除此之外,您必须将数组转换为 Integer []
,因为 int []
不是autoboxed。
If you need any special sorting rules apart from the standard ones, I'm afraid you'd have to use autoboxing. In addition to that, you'd have to transform your array to Integer[]
, because int[]
is not autoboxed.
如果你不是在谈论数组,而是关于集合 - 那么你别无选择 - 集合只能容纳对象。
And if you are not talking about arrays, but about collections - then you have no choice - collections can hold only objects.
这篇关于为什么我不能使用Comparator对基元进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!