考虑到对象是可变的,我正在尝试创建一个复制构造函数。我的复制构造函数是错误的;我似乎无法弄清楚我做错了什么。
请不要告诉我使用clone()
. 在这种情况下,我将如何完成复制构造函数?我是 Java 新手,非常感谢任何帮助。
public class MyList {
public ArrayList<Cool> people;
/**
* "people" variable as a new (empty) ArrayList of Cool objects.
*/
public MyPersonList()
{
people = new ArrayList<Cool>(0);
}
/**
* A copy constructor which makes the right kind of copy considering
* a Cool is mutable.
*/
public MyList(MyList other)
{
people = new ArrayList<Cool>();
for(Cool p:people)
{
people.add(p);
}
}