什么函数可用于对 Vector 进行排序? [英] What function can be used to sort a Vector?
问题描述
我在 java API 中找不到任何用于向量的排序函数.Collections.sort
仅适用于 List
而不适用于 Vector
.
我不想写自己的排序函数,因为我认为java应该实现这个.
我正在寻找类似的东西:
class ClassName 实现 Comparator..类名 cn = ..;排序(cn);
根据 API 文档,Vector
只是实现了List
,所以我没有预见到问题.也许你的困惑是因为你根据旧的 Java 1.0 风格声明了 Vector
:
Vector vector = new Vector();
而不是在接口中声明它(这被认为是很好的做法):
List list = new Vector();
因此您可以使用 Collections#sort()
对集合进行排序,Comparable
定义默认排序行为和/或 Comparator
定义外部可控排序行为.
这是关于订购对象的 Sun 教程.
这是另一个带有完整代码示例的 SO 答案.>
也就是说,你为什么仍然坚持使用 legacy Vector
类?如果可以,只需替换为改进的 ArrayList
十多年前被设计为替换 Vector
.
I cant find any sorting function in the java API for vectors.
Collections.sort
is only for List<T>
and not for Vector<T>
.
I don't want to write my own sorting function because I think java should implement this.
I'm looking for something like:
class ClassName implements Comparator<ClassName> ..
ClassName cn = ..;
sort(cn);
As per the API docs, Vector
just implements List
, so I don't forsee problems. Maybe your confusion was caused because you declared Vector
according the old Java 1.0 style:
Vector vector = new Vector();
instead of the declaring it aginst the interface (which is considered good practice):
List list = new Vector();
You can thus just make use of Collections#sort()
to sort a collection, Comparable
to define the default ordering behaviour and/or Comparator
to define an external controllable ordering behaviour.
Here's a Sun tutorial about ordering objects.
Here's another SO answer with complete code examples.
That said, why are you still sticking to the legacy Vector
class? If you can, just replace by the improved ArrayList
which was been designed as replacement of Vector
more than a decade ago.
这篇关于什么函数可用于对 Vector 进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!