我实现了此处描述的复制构造函数。但问题仍然是,当我更新时route_copy
,同样的更新也适用于route
. 所以,我不明白我的代码有什么问题?
public class Route implements Comparable<Route> {
private List<Site> sites;
public Route()
{
sites = new ArrayList<Site>();
}
public Route(List<Site> sites)
{
this.sites = sites;
}
/**
* Copy constructor
*/
public Route(Route r) {
this(r.sites);
}
public void deleteSite(Site s) {
this.sites.remove(s);
}
}
public processData(Route route)
{
Route route_copy = new Route(route);
Site s = selectSite(route_copy);
route_copy.deleteSite(s); // !!! now 'route' does not contain an element 's'
}