Copy an object containing an array

北慕城南 提交于 2019-12-13 09:29:02

问题


How can I copy an object containing an array of other objects? Here is my copy constructor:

public university(university univ)    {
    String name1=univ.getname();
    int numOfPublications1=univ.getnumOfPublications(); 
    Department[] depts1 =new Department[numOfDepts];
    depts1=univ.getdepts();
}  

This is not working because the array is not there.


回答1:


Try doing this.

public university(university univ)    {
        String name1=univ.getname();
        int numOfPublications1=univ.getnumOfPublications(); 
        Department[] depts1 =new Department[numOfDepts];
        System.arrayCopy(univ.getdepts(),0,depts1,0,univ.getdepts().length);
//or this depts1 = Array.copyOf(univ.getdepts(),numOfDepts);
    }



回答2:


You can use System.arraycopy() to copy the contents of an array.

class University {
  Department[] departments;

  University(University toCopy) {
    departments = new Department[toCopy.departments.length];
    System.arraycopy(toCopy.departments, 0, departments, 0, departments.length);
  }
}



回答3:


Copy constructors have a lot of problems, and in general, cloning is preferable, but if you do want to use a copy constructor here is how:

public University( University univ ){
    University univ_copy = new University();
    univ_copy.name = univ.getname();
    univ_copy.numOfPublications = univ.getnumOfPublications();
    univ_copy.departments = new Department[univ.numOfDepartments];
    System.arrayCopy( univ.departments, 0, univ_copy.departments, 0, univ.departments.length );
    return univ_copy;
}

You can also copy the departments one by one, as opposed to copying them in one step with array copy. The key thing is that you need to allocate a new array for the copy, not re-use the existing array from univ because then you would not have a copy of the array, just a pointer to the original array.




回答4:


To deeply-copy the entire university class, including every element in the Department array, change this:

public university(university univ){
   ...
   Department[] depts1 =new Department[numOfDepts];
   depts1=univ.getdepts();
}

To this:

public university(university univ){
   ...
   depts1 =new Department[univ.depts1.length];
   for(int i = 0; i < univ.depts1.length; i++)  {
      depts[i] = new Department(univ.depts1[i]);
   }

}

This assume that your Department class also has a copy constructor.

Note that I have changed

Department[] depts = ...

to

depts = ...

If you don't do this, then the new depts array will stop existing at the end of your constructor.




回答5:


Besides that you do not follow coding conventions, making your code completely unreadable, you should copy (which I assume you're trying to do, since you feed an instance of university argument to the university constructor) the departments instead of first creating an array of departments that you then immediately scrap and reassign the to argument's departments;

//Get departments from argument 
Department[] departments = univ.getdepts();

//Create your own local departments, if any
if (departments)
{
    this.departments = new Department[departments.length];

    //Copy, either cloned versions or references depending on what you want, for each and every cell in the argument university's department
    for (int i = 0; i < departments.length; i++)
        this.departments[i] = departments[i];
}


来源:https://stackoverflow.com/questions/22103771/copy-an-object-containing-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!