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