可以使用什么函数对Vector进行排序? [英] What function can be used to sort a Vector?

查看:104
本文介绍了可以使用什么函数对Vector进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在java API中找到矢量的任何排序函数。
Collections.sort 仅适用于列表< T> 而不适用​​于 Vector< ; T>

I cant find any sorting function in the java API for vectors. Collections.sort is only for List<T> and not for Vector<T>.

我不想编写自己的排序函数,因为我认为java应该实现它。

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);


推荐答案

根据 API docs Vector 只是实现列表,所以我不会预见到问题。也许您的混淆是因为您根据旧的Java 1.0样式声明 Vector

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();

您可以这样使用 集合#sort() 对集合进行排序, Comparable 定义默认排序行为和/或 比较器 定义外部可控订购行为。

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.

这是 Sun教程订购物品。

这是另一个 SO回答完整的代码示例。

Here's another SO answer with complete code examples.

这就是说,为什么你还坚持遗产 Vector 类?如果可以,只需用改进的 <$ c替换$ c> ArrayList 十多年前被设计为替换 Vector

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆