所以我正在研究一個通用的Graph包,並且我嘗試使用這種方法(我不允許改變)基於它們的自然排序對我的一組邊進行排序:未知的比較器接口錯誤Java
/** Returns the natural ordering on T, as a Comparator. For
* example, if stringComp = Graph.<Integer>naturalOrder(), then
* stringComp.compare(x1, y1) is <0 if x1<y1, ==0 if x1=y1, and >0
* otherwise. */
public static <T extends Comparable<? super T>> Comparator<T> naturalOrder() {
return new Comparator<T>() {
@Override
public int compare(T x1, T x2) {
return x1.compareTo(x2);
}
};
}
我有一個HashSet來保存我所有的邊緣,我想對它們進行排序,根據這種方法, 所以我目前做的(迭代基本上是一個迭代器包裝類):
/** Returns an iterator over all edges in me. If _ordered, return an iterator
over the edges in their natural ordering. */
public Iteration<Edge> edges() {
if (_ordered) {
List<Edge> list = new ArrayList<Edge>(edges);
Collections.sort(list, naturalOrder());
return Iteration.iteration(list.listIterator());
} else {
return Iteration.iteration(edges);
}
}
但Eclipse在.sort下給我下面的錯誤:
The method sort(List<T>, Comparator<? super T>) in the type Collections
is not applicable for the arguments (List<Graph<VLabel,ELabel>.Edge>,
Comparator<Comparable<? super Comparable<? super T>>>)
我真的不明白這意味着什麼,也不知道如何解決它。任何人都可以擺脫一些關於這種情況的光明嗎?
編輯:下面有關於類的一些更多的信息(藉口關閉縮進):
/** Represents a general graph whose vertices are labeled with a type
* VLABEL and whose edges are labeled with a type ELABEL. The
* vertices are represented by the inner type Vertex and edges by
* inner type Edge. A graph may be directed or undirected. For
* an undirected graph, outgoing and incoming edges are the same.
* Graphs may have self edges and may have multiple edges between vertices.
*
* The vertices and edges of the graph, the edges incident on a
* vertex, and the neighbors of a vertex are all accessible by
* iterators. Changing the graph's structure by adding or deleting
* edges or vertices invalidates these iterators (subsequent use of
* them is undefined.)
*/
public abstract class Graph<VLabel, ELabel> {
/** Represents one of my vertices. */
public class Vertex {
/** A new vertex with LABEL as the value of getLabel(). */
Vertex(VLabel label) {
_label = label;
}
/** Returns the label on this vertex. */
public VLabel getLabel() {
return _label;
}
@Override
public String toString() {
return String.valueOf(_label);
}
/** The label on this vertex. */
private final VLabel _label;
}
/** Represents one of my edges. */
public class Edge {
/** An edge (V0,V1) with label LABEL. It is a directed edge (from
* V0 to V1) in a directed graph. */
Edge(Vertex v0, Vertex v1, ELabel label) {
_label = label;
_v0 = v0;
_v1 = v1;
}
/** Returns the label on this edge. */
public ELabel getLabel() {
return _label;
}
/** Return the vertex this edge exits. For an undirected edge, this is
* one of the incident vertices. */
public Vertex getV0() {
return _v0;
}
/** Return the vertex this edge enters. For an undirected edge, this is
* the incident vertices other than getV1(). */
public Vertex getV1() {
return _v1;
}
/** Returns the vertex at the other end of me from V. */
public final Vertex getV(Vertex v) {
if (v == _v0) {
return _v1;
} else if (v == _v1) {
return _v0;
} else {
throw new
IllegalArgumentException("vertex not incident to edge");
}
}
@Override
public String toString() {
return String.format("(%s,%s):%s", _v0, _v1, _label);
}
/** Endpoints of this edge. In directed edges, this edge exits _V0
* and enters _V1. */
private final Vertex _v0, _v1;
/** The label on this edge. */
private final ELabel _label;
}
請問Edge是否實現'Comparable'?發佈其代碼。 –
您想使用它們的自然順序對邊進行排序,但它們沒有定義任何自然順序。 –