Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
219 views
Welcome To Ask or Share your Answers For Others

1 Answer

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);
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...