Home java Deep copying of the array of Java objects

Deep copying of the array of Java objects

Author

Date

Category

How can I use massive objects (own, not string , integer , etc.) copy it to another array with copies of its fields? I use System.ArrayCopy I have no problem copied by the second array, but the problem is that 2 different arrays refer to the same data (the same area of ​​memory), and ⇒ when changing the variable In one array – it changes in the second. How to avoid this? Example:

Public Class MyClass {
  Public Static Void Main (String Args []) {
    MAS MAS1 = NEW MAS ();
    MAS1.NUM = 1;
    Mas Mas2 = New MAS ();
    MAS2.NUM = 2;
    MAS MAS3 = NEW MAS ();
    MAS3.NUM = 3;
    MAS [] Massiv = New Mas [3];
    Massiv [0] = MAS1;
    Massiv [1] = Mas2;
    Massiv [2] = Mas3;
    System.out.PrintLN (Massiv [0] .num + "" + Massiv [1] .num + "" + Massiv [2] .num);
    MAS [] Massiv2 = New Mas [3];
    System.ArrayCopy (Massiv, 0, Massiv2, 0, 3);
    System.out.PrintLN (Massiv2 [0] .num + "" + Massiv2 [1] .num + "" + Massiv2 [2] .num);
    Massiv2 [0] .num = 4;
    System.out.PrintLN (Massiv [0] .num + "" + Massiv [1] .num + "" + Massiv [2] .num);
    System.out.PrintLN (Massiv2 [0] .num + "" + Massiv2 [1] .num + "" + Massiv2 [2] .num);
  }
}
Class MAS {
  int num;
}

Conclusion:

1 2 3
1 2 3.
4 2 3.
4 2 3.

Answer 1

You must copy each element of the array. You cannot copy the array as a whole. Perhaps you heard the term “Deep Copy” .

You will need a FOR cycle, no matter how ugly. And already in the cycle copy each object separately.


Answer 2, Authority 100%

The native System.ArrayCopy does not perform ” deep copying “array, as well as Method Arrays.copyof . Elements from the old array are copied to the new array, i.e. links to objects. Deep copying of nested objects, including arrays, need to be implemented independently taking into account the necessary copying depths, you can also use serialization.


Example of the algorithm “Deep copying” Array. The new array is copied not to references to elements from the source array, but new objects are created:

Static Class Element {
  int num;
  Public Element (int num) {
    this.num = num;
  }
  @Override
  Public String Tostring () {
    Return String.Valueof (NUM);
  }
}
Public Static Void Main (String [] Args) {
  Element [] Arr = New Element [3];
  Arr [0] = New Element (1);
  Arr [1] = New Element (2);
  Arr [2] = New Element (3);
  Element [] Arr2 = New Element [3];
  Intstream.range (0, 3) .Foreach (i - & gt; arr2 [i] = New Element (Arr [i] .num));
  arr2 [0] .num = 4;
  System.out.PrintLN (Arrays.tostring (ARR)); // [1, 2, 3]
  System.out.PrintLN (Arrays.Tostring (ARR2)); // [4, 2, 3]
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions