amazing...
Shallow Copyã¨Deep Copyã®éã
Shallow Copyã¨Deep Copyã®éãã«ã¤ãã¦ã¯ã
⧠ä¸å³ãã¤ã¡ã¼ã¸ãããããã¨ã
ä¸å¿ãWikipediaããæç²ããã¨ã
One method of copying an object is the shallow copy. In that case a new object B is created, and the fields values of A are copied over to B. This is also known as a field-by-field copy, field-for-field copy, or field copy. If the field value is a reference to an object (e.g., a memory address) it copies the reference, hence referring to the same object as A does, and if the field value is a primitive type it copies the value of the primitive type. In languages without primitive types (where everything is an object), all fields of the copy B are references to the same objects as the fields of original A. The referenced objects are thus shared, so if one of these objects is modified (from A or B), the change is visible in the other. Shallow copies are simple and typically cheap, as they can usually be implemented by simply copying the bits exactly.
An alternative is a deep copy, meaning that fields are dereferenced: rather than references to objects being copied, new copy objects are created for any referenced objects, and references to these placed in B. The result is different from the result a shallow copy gives in that the objects referenced by the copy B are distinct from those referenced by A, and independent. Deep copies are more expensive, due to needing to create additional objects, and can be substantially more complicated, due to references possibly forming a complicated graph.
Deep copy is a process in which the copying process occurs recursively. It means first constructing a new collection object and then recursively populating it with copies of the child objects found in the original. In case of deep copy, a copy of object is copied in other object. It means that any changes made to a copy of object do not reflect in the original object. In python, this is implemented using âdeep copy()â function.
⧠éãã¯ããªãã¸ã§ã¯ãã«å¯¾ããå¤æ´ãå ±æããããã©ããã£ã¦ãã¨ã§ãããã
Javaã®æ¨æºAPIã§é åã®Shallow Copyã¨Deep Copy
ãããã®æ å ±ããä¾ã 諤ã ã¨ãã¦ãã¦ã
⧠æ å ±ãé¯ç¶ãéããªãã ãã©ãWikipediaã®ãShallow Copyãã¨ãDeep Copyãã®èª¬æãæ£ããã¨ä»®å®ãã¦ç¢ºèªãããããªãã§ãããã
â java.lang.Object.clone()
package lesson.chapter04; import java.util.Arrays; public class Lesson4_10 { public static void main(String[] args) { // 1次å ã®ã³ãã¼ System.out.println("ã1次å ã®ã³ãã¼ã"); int[] original = {1, 2, 3, 4, 5}; int[] replica = original.clone(); replica[2] = 777; Arrays.stream(original).forEach(i -> System.out.print(i +" ")); System.out.println(); Arrays.stream(replica).forEach(i -> System.out.print(i +" ")); System.out.println(); // 2次å ã®ã³ãã¼ System.out.println("ã2次å ã®ã³ãã¼ã"); int[][] arrayA = {{1, 2}, {1, 2}, {1, 2, 3}}; int[][] arrayB = arrayA.clone(); for (int[] tmp: arrayA) { for (int val: tmp) { System.out.print(val + " "); } } for (int i = 0; i < arrayB.length; i++) { for (int j = 0; j < arrayB[i].length; j++) { arrayB[i][j] = 777; } } System.out.println(); for (int[] tmp: arrayA) { for (int val: tmp) { System.out.print(val + " "); } } System.out.println(); } }
⧠ã®çµæã¯ã
1次å ã®é åã¯ãDeep Copyã§ãã¦ãã£ã½ããã©ã2次å ã®é åã¯ãShallow Copyã£ã½ãæãã«ãªã£ã¦ã模æ§ã
Â
â java.lang.System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
package lesson.chapter04; public class Lesson4_11 { public static void main(String[] args) { // 1次å System.out.println("ã1次å ã®ã³ãã¼ã"); char[] arrayA = {'A', 'B', 'C', 'D', 'E'}; char[] arrayB = new char[arrayA.length]; System.arraycopy(arrayA, 0, arrayB, 0, arrayA.length); for (int i = 0; i < arrayB.length; i++) { arrayB[i] = 'A'; } for (char c: arrayA) { System.out.print(c); } System.out.println(); for (char c: arrayB) { System.out.print(c); } System.out.println(); // 2次å System.out.println("ã2次å ã®ã³ãã¼ã"); char[][] array2A = {{'A', 'B', 'C', 'D', 'E'}, {'A', 'B', 'C', 'D', 'E'}}; char[][] array2B = new char[array2A.length][]; for (int i = 0; i < array2A.length; i++) { array2B[i] = new char[array2A[i].length]; System.arraycopy(array2A[i], 0, array2B[i], 0, array2A[i].length); } for (int i = 0; i < array2B.length; i++) { for (int j = 0; j < array2B[i].length; j++) { array2B[i][j] = 'Z'; } } for (int i = 0; i < array2B.length; i++) { for (int j = 0; j < array2B[i].length; j++) { System.out.print(array2B[i][j]); } System.out.println(); } for (int i = 0; i < array2A.length; i++) { for (int j = 0; j < array2A[i].length; j++) { System.out.print(array2A[i][j]); } System.out.println(); } } }
⧠ã®çµæã¯ã
1次å ã®é åã2次å ã®é åã®ã©ã¡ããDeep Copyã«ãªã£ã¦ããã ãã©ã
ä»åã¯ããªããã£ãåã®é åã§ã®çµæã ãã©ãåç §åã®é åã ã¨éã£ãçµæã«ãªããããªï¼
Object#clone()ã®ã³ãã¼æ¹æ³ã¯ãã·ã£ãã¼ã³ãã¼ï¼shallow copyï¼æµ
ãã³ãã¼ï¼ã¨å¼ã°ãããã®ã
ããã«å¯¾ããã®ããã£ã¼ãã³ãã¼ï¼deep copyï¼æ·±ãã³ãã¼ï¼ã
ã·ã£ãã¼ã³ãã¼ã¯ããªãã¸ã§ã¯ãã®ãã£ã¼ã«ãï¼ã¡ã³ãã¼å¤æ°ï¼ããªãã¸ã§ã¯ãï¼åç
§åï¼ã§ããå ´åã«ããã®åç
§ãã³ãã¼ããã ãã
ã¤ã¾ããã£ã¼ã«ãã®ãªãã¸ã§ã¯ãã¯ãè¤è£½å
ã¨è¤è£½å
ã§åããªãã¸ã§ã¯ããæããã¨ã«ãªãã
ããã«å¯¾ããã£ã¼ãã³ãã¼ã¯ãããã£ã¼ã«ãã®ãªãã¸ã§ã¯ãèªèº«ãè¤åããæ¹å¼ããæãã
ãã£ã¼ãã³ãã¼ãèªåçã«è¡ãã¡ã½ããã¯ç¨æããã¦ããªãã®ã§ããªãã¸ã§ã¯ãã®ãã£ã¼ãã³ãã¼ãä½ãããã®ã§ããã°ããã£ã¼ãã³ãã¼ç¨ã®ã¡ã½ãããèªåã§ä½ãå¿
è¦ãããã
⧠ä¸è¨ãµã¤ãæ§ã®èª¬æãè¦ãã¨ããã¯ãåç §åã®é åã§è©¦ãå¿ è¦ããã£ãæ°ãããã
2022å¹´10æ11æ¥ï¼ç«ï¼è¿½è¨ï¼â ãããã
System.arrayCopy()ã«ã¤ãã¦ãåç §åã®é åã¯ãShallow Copyã«ãªã£ãã£ã½ãã§ãã
â /test-silver/src/lesson/chapter10/Sample.java
package lesson.chapter10; public class Sample { int num; Sample(int num) { this.num = num; } }
â /test-silver/src/lesson/chapter10/Lesson10_64.java
package lesson.chapter10; public class Lesson10_64 { public static void main(String[] args) { Sample[] array = { new Sample(10), new Sample(20) }; Sample[] array2 = new Sample[2]; System.arraycopy(array, 0, array2, 0, array.length); array2[1].num = 10; System.out.println(array[1].num); } }
⧠ã§å®è¡ããã¨ã
ã¨ããããã«é åã®ã¤ã³ã¹ã¿ã³ã¹ãå ±æããã¦ãã...
é åã®è¦ç´ ãããªããã£ãåãåç §åãã§ãSystem.arrayCopy()ã®æåãå¤ããã£ã¦ãã¨ã¿ãããã
2022å¹´10æ11æ¥ï¼ç«ï¼è¿½è¨ï¼â ããã¾ã§
æ¯åº¦ã¢ã¤ã¢ã¤æãå端ãªã...
ä»åã¯ãã®ã¸ãã§ã
Â
Â