Java 什么函数可用于对 Vector 进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2072032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What function can be used to sort a Vector?
提问by Hidayat
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 API 中找不到任何排序函数。
Collections.sort
只为List<T>
,不为Vector<T>
。
I don't want to write my own sorting function because I think java should implement this.
我不想编写自己的排序函数,因为我认为 java 应该实现这一点。
I'm looking for something like:
我正在寻找类似的东西:
class ClassName implements Comparator<ClassName> ..
ClassName cn = ..;
sort(cn);
采纳答案by BalusC
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:
根据API 文档,Vector
只是实现List
,所以我没有预见到问题。也许你的困惑是因为你Vector
根据旧的 Java 1.0 风格声明:
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.
因此,您可以使用 对Collections#sort()
集合进行排序,Comparable
定义默认排序行为和/或Comparator
定义外部可控排序行为。
Here's a Sun tutorialabout ordering objects.
这是有关订购对象的Sun 教程。
Here's another SO answerwith complete code examples.
这是另一个带有完整代码示例的SO 答案。
That said, why are you still sticking to the legacyVector
class? If you can, just replace by the improved ArrayList
which was been designed as replacement of Vector
more than a decade ago.
也就是说,你为什么仍然坚持传统Vector
课程?如果可以,只需替换为十多年前ArrayList
设计为替代品的改进型Vector
。
回答by jsight
Vector implements List, so Collections.sort would work.
Vector 实现了 List,所以 Collections.sort 会起作用。
回答by coobird
According to the Java API Specification for the Vector
class, it implements the List
interface which is needed to use the Collections.sort
method.
根据Vector
类的 Java API 规范,它实现了List
使用该Collections.sort
方法所需的接口。
Also, as a note, for most uses the Vector
class could be replaced by using one of the List
implementations in the Java Collections Framework, such as ArrayList
. The Vector
class is synchronized, so unless there is a real need for synchronized access one should use one of the other List
implementations.
另外,请注意,对于大多数用途,Vector
可以使用List
Java 集合框架中的一种实现来替换该类,例如ArrayList
. 该Vector
班是同步的,所以除非有同步访问应该使用其他的一个真正需要List
的实现。
回答by les2
Vector is a List
向量是一个列表
回答by user2190247
Collections.sort(nameOfTheVectorToBeSorted); try this on your vector, that will get sorted.
Collections.sort(nameOfTheVectorToBeSorted); 在你的载体上试试这个,它会被排序。
回答by Crt
Collections.sort(vector_name)
Collections.sort(vector_name)
It'll sort the vector in place, so you don't need to assign the result of the above command back to the vector.
它将就地对向量进行排序,因此您无需将上述命令的结果分配回向量。
This works because Vectors are implementations of Lists.
这是有效的,因为向量是列表的实现。
回答by JoVroye
Don't forget to add implements Comparable<>
in your class:
不要忘记implements Comparable<>
在您的班级中添加:
public class XXXX
implements Comparable<XXXX> {
}
And to redefine compareTo()
in your class of the object type stored in your vector.
并compareTo()
在您的类中重新定义存储在您的向量中的对象类型。
I got this problem as well, Eclipse IDE was telling me that Collection.sort()
was for List<T>
only. Couldn't make it work until I did what I just said.
我也遇到了这个问题,Eclipse IDE 告诉我这Collection.sort()
只是为了List<T>
。在我按照我刚才说的做之前,我无法让它发挥作用。