Java 和 JGraphT - 不理解结果 - 通过 value/reference 问题或其他问题?

Java and JGraphT - Do not understand result - Pass by value/reference issue or something else?

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());
        }
    }

}
}

输出

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

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

抱歉,如果这段代码没有立即出现运行 - 我已经从未连接到外部世界的系统中手动复制了它

使用覆盖的 toString:

Create Schedule View Jobs 225534817 1 9999
Create Schedule View Jobs 1878246837 0 9999
Create Schedule View Jobs 929338653 2 9999
Create Schedule View Prior Job 1878246837 0 9999
Create Schedule View Prior Job 1259475182 1 9999
Setting 225534817
Setting 1878246837
Setting 929338653
Added Times View Jobs 225534817 1 1111
Added Times View Jobs 1878246837 0 1111
Added Times View Jobs 929338653 2 1111
Added Times View Prior Job 1878246837 0 1111
Added Times View Prior Job 1259475182 1 9999

我弄清楚了我做错了什么。

这与我如何向图中添加顶点和边以及我如何覆盖 hashCode() 和 equals() 有关。

addVertex(Job) 和 addEdge(Job, Job) 仅在图形尚未包含对象时才向图形添加对象。 'Contain' 使用 equals()。

对于 Job1 依赖于 Job0,该图不包含这两个 Job,因此添加了这两个 Job,并按照我的预期在它们之间添加了一条边。

对于 Job2 依赖于 Job1,图表已经包含一个 'old' Job1 对象等于 'new' Job1 对象。此时我有两个 Job1 对象,它们是 equal() 彼此但独立的对象。我创建的边位于 Job2 和 'new' Job1 对象之间,它实际上不在图中!