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
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);
7 个解决方案
#1
28
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,所以我不会预见到问题。也许你的混乱是因为你根据旧的Java 1.0风格声明了Vector:
Vector vector = new Vector();
instead of the declaring it aginst the interface (which is considered good practice):
而不是声明它aginst接口(这被认为是良好的做法):
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 tutorial about ordering objects.
这是一个关于订购对象的Sun教程。
Here's another SO answer with complete code examples.
这是完整代码示例的另一个SO答案。
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类?如果可以,只需用十多年前被设计为Vector替换的改进的ArrayList替换。
#2
15
Vector implements List, so Collections.sort would work.
Vector实现List,所以Collections.sort可以工作。
#3
4
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规范,它实现了使用Collections.sort方法所需的List接口。
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.
另外,作为注释,对于大多数用途,可以使用Java Collections Framework中的List实现之一替换Vector类,例如ArrayList。 Vector类是同步的,因此除非确实需要同步访问,否则应该使用其他List实现之一。
#4
2
Vector is a List
矢量是一个列表
#5
1
Collections.sort(nameOfTheVectorToBeSorted); try this on your vector, that will get sorted.
Collections.sort(nameOfTheVectorToBeSorted);在你的向量上尝试这个,它将被排序。
#6
1
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
#7
0
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.
这是因为Vectors是列表的实现。
#1
28
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,所以我不会预见到问题。也许你的混乱是因为你根据旧的Java 1.0风格声明了Vector:
Vector vector = new Vector();
instead of the declaring it aginst the interface (which is considered good practice):
而不是声明它aginst接口(这被认为是良好的做法):
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 tutorial about ordering objects.
这是一个关于订购对象的Sun教程。
Here's another SO answer with complete code examples.
这是完整代码示例的另一个SO答案。
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类?如果可以,只需用十多年前被设计为Vector替换的改进的ArrayList替换。
#2
15
Vector implements List, so Collections.sort would work.
Vector实现List,所以Collections.sort可以工作。
#3
4
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规范,它实现了使用Collections.sort方法所需的List接口。
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.
另外,作为注释,对于大多数用途,可以使用Java Collections Framework中的List实现之一替换Vector类,例如ArrayList。 Vector类是同步的,因此除非确实需要同步访问,否则应该使用其他List实现之一。
#4
2
Vector is a List
矢量是一个列表
#5
1
Collections.sort(nameOfTheVectorToBeSorted); try this on your vector, that will get sorted.
Collections.sort(nameOfTheVectorToBeSorted);在你的向量上尝试这个,它将被排序。
#6
1
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
#7
0
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.
这是因为Vectors是列表的实现。