java - `Arrays,copyOf` didn't do a deep copy of objects as it suppose to -


i have classbook can have many authors. pass few objects of class author constructor of class book copy these objects arrays.copyof if change author data outside object change everywhere means copy has not been created.

i create deep copy. code compiling , running no errors.

import java.util.arrays;  public class book {     private string tytul;     private int rokwydania;     private author[] autorzy;      public book(string tytul, int rokwydania, author... autorzy) {         this.tytul = tytul;         this.rokwydania = rokwydania;         this.autorzy = arrays.copyof(autorzy, autorzy.length);     }      public string tostring() {         string s = "      tytuł: " + tytul +"\nrok wydania: " + rokwydania + "\n";         if(autorzy.length == 1)             s+="      autor: " + autorzy[0];         else{             s+="    autorzy: " + autorzy[0];             for(int = 1; < autorzy.length; i++)                 s+="\n             " + autorzy[i];         }                return s + "\n\n";     }      public static void main(string[] args) {         author a1 = new author("isabel", "allende", 1942);         author a2 = new author("manueala", "gretkowska", 1964);         author a3 = new author("piotr", "pietucha", 1954);          book b1 = new book("suma naszych dni", 2010, a1);         book b2 = new book("polka", 2001, a2);         book b3 = new book("sceny z życia pozamałżeńskiego", 2003, a2, a3);          a2.zmieninformacje("tove", "jansson", 1956);         book b4 = new book("lato muminków", 2006, a2);          system.out.println(b1);          system.out.println(b2);          system.out.println(b3);          system.out.println(b4);     } } 

public class author {     private string imie, nazwisko;     private int rokurodzenia;      public author(string imie, string nazwisko, int rokurodzenia) {         this.imie = imie;         this.nazwisko = nazwisko;         this.rokurodzenia = rokurodzenia;     }      public string getimie() {         return imie;     }      public string getnazwisko() {         return nazwisko;     }      public int getrokurodzenia() {         return rokurodzenia;     }      @override     public string tostring() {         return  imie + " " + nazwisko + " (ur. " + rokurodzenia + ")";     }      public void zmieninformacje(string imie, string nazwisko, int rokurodzenia) {         this.imie = imie;         this.nazwisko = nazwisko;         this.rokurodzenia = rokurodzenia;      }  } 

desired output:

     tytuł: suma naszych dni   rok wydania: 2010         autor: isabel allende (ur. 1942)           tytuł: polka   rok wydania: 2001         autor: manuela gretkowska (ur. 1964)           tytuł: suma naszych dni   rok wydania: 2010       autorzy: manuela gretkowska (ur. 1964)                   piotr pietucha (ur. 1954)           tytuł: lato muminków   rok wydania: 2006         autor: tove jansson (ur. 1956) 

my output

      tytuł: suma naszych dni rok wydania: 2010       autor: isabel allende (ur. 1942)         tytuł: polka rok wydania: 2001       autor: tove jansson (ur. 1956)//wrong         tytuł: sceny z życia pozamałżeńskiego rok wydania: 2003     autorzy: tove jansson (ur. 1956)//wrong              piotr pietucha (ur. 1954)         tytuł: lato muminków rok wydania: 2006       autor: tove jansson (ur. 1956) 

i think arrays.copyof working fine. issue in line a2.zmieninformacje("tove", "jansson", 1956); updating author a2 before printing book b2 , b3 .


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -