大家好我正在尝试深度复制类A.我无法深度复制类A.订阅是一个包含SubscriptionYear对象的数组引用。有人可以帮我使用克隆方法进行深层复制。
public class A {
private String countryNames;
private SubscriptionYear[] subscriptions;
private int size;
private int location;
public A(String country)
{
this.countryNames = country;
}
public A(String country, int arraylength)
{
this.countryNames = country;
this.size = arraylength;
subscriptions = new SubscriptionYear[size];
location = 0;
}
//adds the subscription.
public void addSubscriptionYear(int year, double subscription)
{
subscriptions[location]= new SubscriptionYear(year, subscription);
++location;
}
订阅年份:
public class SubscriptionYear {
private int year;
private double subscriptions;
public SubscriptionYear(int year,double subscriptions)
{
setYear(year);
setSubscription(subscriptions);
}
public void setYear(int Year)
{
this.year= Year;
}
public void setSubscription(double value)
{
this.subscriptions = value;
}
public int getYear()
{
return year;
}
public double getSubscription()
{
return subscriptions;
}
答案 0 :(得分:0)
public Object clone() {
A newA = new A(countryNames);
newA.size = size;
newA.subscriptions = new SubscriptionYear[size];
for(SubscriptionYear subYear : subscriptions) {
newA.addSubscriptionYear(subYear.getYear(), subYear.getSubscription());
}
return newA;
}
答案 1 :(得分:0)
好的,我解决了我的问题。这个解决方案解决了我的问题:
public Object clone() {
A newA = new A(this.countryNamse, this.arraylength);
for(int i=0;i<this.newA.subscriptions.length;i++) {
newA.subscriptions[i] = subscriptions[i[];
}
return newA;
}