Shallow Copy(얕은 복사)
1) 소스와 타겟이 모두 추가/변경에 영향을 받음
ArrayList<Node> sourceList = new ArrayList<>();
ArrayList<Node> targetList = sourceList;
아래 2),3),4)는 추가는 소스만, 변경은 소스와 타켓 모두 영향을 받음
2)
ArrayList<Node> sourceList = new ArrayList<>();
ArrayList<Node> targetList = new ArrayList<>(sourceList);
3)
ArrayList<Node> targetList = new ArrayList<>();
targetList.addAll(sourceList);
4)
ArrayList<Node> sourceList = new ArrayList<>();
ArrayList<Node> targetList = (ArrayList<Node>)sourceList.clone();
2.Deep Copy(깊은 복사)
5)소스와 타겟의 데이터 추가 및 변경이 독립적
ArrayList<Node> sourceList = new ArrayList<>();
ArrayList <Node> targetList = new ArrayList<>();
for (Node node : sourceList) {
Node newNode = new Node(node.data, node.name);
targetList.add(newNode);
}
import java.util.ArrayList;
class Node {
int data;
String name;
Node(int data, String name) {
this.data = data;
this.name = name;
}
public String toString() {
return "{" + Integer.toString(data) + ", " + name + "}";
}
}
public class ArrayListTest {
public static void main(String[] args) {
testCopy1();
System.out.println("===============\n\n");
testCopy2();
System.out.println("===============\n\n");
testCopy3();
System.out.println("===============\n\n");
testCopy4();
System.out.println("===============\n\n");
testCopy5();
}
static void testCopy1(){
ArrayList<Node> sourceList = new ArrayList <Node>();
Node node[] = new Node[5];
for(int i = 0; i < 5; i++) {
node[i] = new Node(i, Integer.toString(i));
}
sourceList.add(node[0]);
sourceList.add(node[1]);
sourceList.add(node[2]);
System.out.println("Before1: ");
System.out.println("sourceList = " + sourceList.toString());
ArrayList <Node> targetList = sourceList;
System.out.println("targetList = " + targetList.toString());
sourceList.add(node[3]);//source, target 추가됨
node[2].data = 10;
node[2].name = "10";//source, target 변경됨
System.out.println("\nAfter: ");
System.out.println("sourceList = " + sourceList.toString());
System.out.println("targetList = " + targetList.toString());
}
static void testCopy2(){
ArrayList<Node> sourceList = new ArrayList <>();
Node node[] = new Node[5];
for(int i = 0; i < 5; i++) {
node[i] = new Node(i, Integer.toString(i));
}
sourceList.add(node[0]);
sourceList.add(node[1]);
sourceList.add(node[2]);
System.out.println("Before2: ");
System.out.println("sourceList = " + sourceList.toString());
ArrayList <Node> targetList = new ArrayList <> (sourceList);
System.out.println("targetList = " + targetList.toString());
sourceList.add(node[3]);//source만 추가됨
node[2].data = 20;
node[2].name = "20";//source, target변경됨
System.out.println("\nCurrent2: ");
System.out.println("sourceList = " + sourceList.toString());
System.out.println("targetList = " + targetList.toString());
}
static void testCopy3(){
ArrayList<Node> sourceList = new ArrayList <>();
Node node[] = new Node[5];
for(int i = 0; i < 5; i++) {
node[i] = new Node(i, Integer.toString(i));
}
sourceList.add(node[0]);
sourceList.add(node[1]);
sourceList.add(node[2]);
System.out.println("Before3: ");
System.out.println("sourceList = " + sourceList.toString());
ArrayList <Node> targetList = new ArrayList <> ();
targetList.addAll(sourceList);
System.out.println("targetList = " + targetList.toString());
sourceList.add(node[3]);
node[2].data = 30;
node[2].name = "30";
System.out.println("\nAfter3: ");
System.out.println("sourceList = " + sourceList.toString());
System.out.println("targetList = " + targetList.toString());
}
static void testCopy4(){
ArrayList<Node> soureList = new ArrayList <Node>();
Node node[] = new Node[5];
for(int i = 0; i < 5; i++) {
node[i] = new Node(i, Integer.toString(i));
}
soureList.add(node[0]);
soureList.add(node[1]);
soureList.add(node[2]);
System.out.println("Before4: ");
System.out.println("sourceList = " + soureList.toString());
ArrayList <Node> targetList = (ArrayList <Node>)soureList.clone();
System.out.println("targetList = " + targetList.toString());
soureList.add(node[3]);
node[2].data = 40;
node[2].name = "40";
System.out.println("\nAfter4: ");
System.out.println("sourceList = " + soureList.toString());
System.out.println("targetList = " + targetList.toString());
}
static void testCopy5(){
ArrayList<Node> sourceList = new ArrayList <>();
Node n[] = new Node[5];
for(int i = 0; i < 5; i++) {
n[i] = new Node(i, Integer.toString(i));
}
sourceList.add(n[0]);
sourceList.add(n[1]);
sourceList.add(n[2]);
System.out.println("Before5: ");
System.out.println("sourceList = " + sourceList.toString());
ArrayList <Node> targetList = new ArrayList<>();
for (Node node : sourceList) {
Node newNode = new Node(node.data, node.name);
targetList.add(newNode);
}
System.out.println("targetList = " + targetList.toString());
sourceList.add(n[3]);//sourceList 만추가
n[2].data = 50;
n[2].name = "50";//sourceList만 변경
System.out.println("\nAfter5: ");
System.out.println("sourceList = " + sourceList.toString());
System.out.println("targetList = " + targetList.toString());
}
}
'개발' 카테고리의 다른 글
Doxygen (문서화 툴) (0) | 2009.07.20 |
---|