findCycle() - это поиск петли, в которой объект Streetsection не используется дважды. Теперь, если я делаю nextStreetSection.setMarked(), является ли это StreetSection currentPlace, который получает отметку или это только локальная переменная nextStreetSection? Если это только локальная переменная, то список разделов StreetSections вообще не изменится, маркировка не изменится. С другой стороны, setMarked() ссылается на nextStreetSection, поэтому почему он должен влиять на List в currentPlace.Переменное распределение и выделение изменений Объекты
Извините за длинный код, я хотел быть уверенным, что контекст ясен. Также im new здесь :) Спасибо.
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>();
}
}
Большое спасибо! – Grunwalski
Хорошо, я подумал об этом. Если все в java передается по значению, то почему изменяется значение без знака StreetSection currentPlace. Поскольку nextStreetSection - это всего лишь копия его значения? – Grunwalski
Только примитивы передаются по значению, объекты передаются по ссылке. –