Java和JGraphT - 不理解结果 - 通过值/引用问题或其他什么?

时间:2016-08-02 15:56:56

标签: java jgrapht

threading

输出

import org.jgrapht.*;
import org.jgrapht.graph.*;

public class Example {

// Cut down version of Job class
private static class Job {
    private final int jobNumber;
    private int jobTime;

    Job(int jobNumber){
        this.jobNumber = jobNumber;
        this.jobTime = 9999; // A dummy value to make my issue obvious
    }

    public int getJobNumber(){
        return jobNumber;
    }

    public int getJobTime(){
        return jobTime;
    }

    public void setJobTime(int jobTime){
            this.jobTime=jobTime;
        }

    // eclipse derived
    @Override
    public int hashCode(){
        final int prime = 31;
        int result = 1;
        result = prime * result + jobNumber ;
        return result;
    }

    // eclipse derived
    @Override
    public boolean equals(Object obj) {
        if (this ==obj) return true;
        if (obj==null) return false;
        if (getClass() != obj.getClass()) return false;
        Job other = (Job) obj;
        if (jobNumber != other.jobNumber) return false;
        return true;
    }

    @Override
    public String toString() {
        return Integer.toString(System.identityHashCode(this));
    }


}

public static void main(String[] args) {

    // Create a graph
    final DirectedGraph<Job, DefaultEdge> schedule = new DefaultDirectedGraph<Job,DefaultEdge>(DefaultEdge.class);

    // Create some jobs and add to graph
    // Job 2 depends on Job 1 depends on Job 0
    Job job;
    Job dependsOnJob;
    int counter=1;
    while (counter < 3) {
        int jobNumber = counter;
        int dependsOnJobNumber = counter - 1;

        job = new Job(jobNumber);
        dependsOnJob = new Job(dependsOnJobNumber);

        schedule.addVertex(job);
        schedule.addVertex(dependsOnJob);
        schedule.addEdge(dependsOnJob,job);

        counter++;          
    }

    // Print the current values of job numbers and times
    // 1.
    for (Job j : schedule.vertexSet()){
        System.out.println("Create Schedule View Jobs " + j + " " + j.getJobNumber() + " " + j.getJobTime());
    }
    // 2.
    for (Job j : schedule.vertexSet()){
        for (DefaultEdge e : schedule.incomingEdgesOf(j)){
            Job source = schedule.getEdgeSource(e);
            System.out.println("Create Schedule View Prior Job " + source + " " + source.getJobNumber() + " " + source.getJobTime());
        }
    }

    // Change all job times to 1111
    for (Job j : schedule.vertexSet()){
        System.out.println("Setting " + j);
        j.setJobTime(1111);
    }

    // Print the new values of job numbers and times
    // 3.
    for (Job j : schedule.vertexSet()){
        System.out.println("Added Times View Jobs " + j + " " + j.getJobNumber() + " " + j.getJobTime());
    }
    // 4.
    for (Job j : schedule.vertexSet()){
        for (DefaultEdge e : schedule.incomingEdgesOf(j)){
            Job source = schedule.getEdgeSource(e);
            System.out.println("Added Times View Prior Job " + source + " " + source.getJobNumber() + " " + source.getJobTime());
        }
    }

}
}

在步骤4中,并非作业时间的所有值都显示为已从9999更改为1111,尽管步骤3显示它们已有。我在谈论上面输出的最后一行。有人能帮我理解发生了什么吗?这让我发疯了!

如果此代码没有立即运行,请道歉 - 我已经从未连接到外部世界的系统手动复制它

使用重写的toString:

Create Schedule View Jobs Example$Job@20 1 9999
Create Schedule View Jobs Example$Job@1f 0 9999
Create Schedule View Jobs Example$Job@21 2 9999
Create Schedule View Prior Job Example$Job@1f 0 9999
Create Schedule View Prior Job Example$Job@20 1 9999
Setting Example$Job@20
Setting Example$Job@1f
Setting Example$Job@21
Added Times View Jobs Example$Job@20 1 1111
Added Times View Jobs Example$Job@1f 0 1111
Added Times View Jobs Example$Job@21 2 1111
Added Times View Prior Job Example$Job@1f 0 1111
Added Times View Prior Job Example$Job@20 1 9999

1 个答案:

答案 0 :(得分:0)

我弄清楚我做错了什么。

这与我如何向图形添加顶点和边以及如何重写hashCode()和equals()有关。

addVertex(作业)和addEdge(作业,作业)仅在图形尚未包含图形时才向图形添加对象。 &#39;含有&#39;使用equals()。

对于Job1依赖于Job0,图表中不包含这些作业,因此两者都被添加,并且它们之间的边缘也是我想要的。

对于Job2取决于Job1,该图表已经包含了一个旧的&#39; Job1对象等于&#39; new&#39; Job1对象。此时我有两个Job1对象,它们彼此相等()但是单独的对象。我创造的边缘是在Job2和&#39; new&#39;之间。 Job1对象,它实际上不在图中!