变量分配和更改分配的对象

Variable Allocation and the change allocated Objects

findCycle() 是关于寻找一个 Streetsection 没有被使用两次的循环。现在,如果我执行 nextStreetSection.setMarked(),它是 StreetSection 吗? 被标记的 currentPlace 还是只是局部变量 nextStreetSection ?如果它只是局部变量,那么 Place 的 streetSections 列表根本不会改变,标记也不会改变。另一方面 setMarked() 指的是 nextStreetSection ,那么为什么它会影响 currentPlace.

中的列表

抱歉代码太长,我想确保上下文清楚。 我也是新来的:) 谢谢。

public Cycle findCycle(Place start)
{
    Cycle cycle = new Cycle();
    Place currentPlace = start;
    boolean done = false;
    if (start.getUnmarkedStreetSection() == null)
    {
        cycle.addPlace(start);
        done = true;
    }
    while (!done)
    {
        cycle.addPlace(currentPlace);
        StreetSection nextStreetSection = currentPlace.getUnmarkedStreetSection();
        nextStreetSection.setMarked();
        currentPlace = nextStreetSection.getOtherEnd(currentPlace);
        done = (currentPlace == start);
    }
    return cycle;
}


public StreetSection getUnmarkedStreetSection()
{
    for (StreetSection streetSection : streetSections)
    {
        if (!streetSection.isMarked())
            return streetSection;
    }
    return null;
}


public class StreetSection
{
    private Place place1, place2;
    private boolean marked;
    public StreetSection(Place place1, Place place2)
    {
        this.place1 = place1;
        this.place2 = place2;
        this.marked = false;
    }
}


public class Place
{
    private String name;
    private LinkedList<StreetSection> streetSections;

    public Place(String name)
    {
        this.name = name;
        this.streetSections = new LinkedList<StreetSection>();
    }
}

链接当前 Place 和下一个 PlaceStreetSection 被标记。

因此,这会影响所有 Place 在其自身 LinkedList<StreetSection> streetSections 中具有此 StreetSection 的对象。