我正在使用一些从在线来源获得的代码,用于化学建模项目的图论部分。我想弄清楚这一点。关于类决定哪一个是整体,第一行代码意味着什么?一个是第一个顶点,两个是类的第二个顶点。我不精通线性代数/离散数学,所以如果可能的话,请避免数学上的激烈解释。
public Edge(Vertex one, Vertex two, int length){
this.one = (one.getElement().compareTo(two.getElement()) <= 0) ? one : two;
this.two = (this.one == one) ? two : one;
this.length = length;
}
谢谢!
它写得不好。两个测试,一个可以做,几乎故意默默无闻。它只是试图将较小的顶点指定给one
,将另一个指定给two
,以保持边的顺序一致。更清晰的版本是:
public Edge(Vertex one, Vertex two, int length)
{
if (one.getElement().compareTo(two.getElement()) <= 0)
{
this.one = one;
this.two = two;
}
else
{
this.one = two;
this.two = one;
}
this.length = length;
}
不确定您在这里要做什么,但第一行是使用Comparable接口,无论Vertex.getElement()
返回什么,都会实现该接口。如果one
的元素"小于或等于"two
的元素,则this
边缘的one
被设置为给定的one
,否则被设置为two
。第二行然后相应地设置this
的two
(即,如果我们决定this.one = one
,则this.two = two
,否则this.one = two
和this.two = one
)。