0

findCycle() is about finding a Loop in which a Streetsection is not used twice. Now if i do nextStreetSection.setMarked(), is it the StreetSection of currentPlace that is getting marked or is it just the local variable nextStreetSection ? If its just the local variable, then the streetSections List of Place wont change at all, the markings wont change. On the other hand setMarked() is referring to nextStreetSection , so why should it affect the List in currentPlace.

Sorry for the long code, i wanted to be sure that the context is clear. Also im new here :) Thanks.

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>();
    }
}
Mike Tunnicliffe
  • 10,674
  • 3
  • 31
  • 46
Grunwalski
  • 129
  • 5

1 Answers1

0

It is the StreetSection that links the current Place and the next Place that gets marked.

This therefore affects all of the Place objects that have this StreetSection in their own LinkedList<StreetSection> streetSections.

Mike Tunnicliffe
  • 10,674
  • 3
  • 31
  • 46
  • Ok i have thought about this. If everything in java is passed by value,so why does the unmarked StreetSection of currentPlace change. Since nextStreetSection is just a copy of the value of it ? – Grunwalski Mar 15 '15 at 09:40
  • Only primitives are passed by value, objects are passed by reference. – Mike Tunnicliffe Mar 15 '15 at 22:28