---
title: "Java的深拷贝和浅拷贝--clone()方法（二） "
author: "tessykandy"
date: "2013-05-02T17:58:13Z"
tags: ["Java", "clone", "拷贝", "深拷贝"]
categories: ["编程语言"]
slug: "java-clone"
draft: false
lastmod: "2013-05-02T18:06:44Z"
id: 1858865
url: "https://tessykandy.iteye.com/blog/1858865"
---

预定义集合类型--clone方法：实际是逐个调用每个元素的operator=方法

因此，预定义集合类型的clone()结果为浅拷贝还是深拷贝，取决于其元素类型

1\. 元素为预定义非集合类型

```
	private void testClone() {
		ArrayList<String> listA = new ArrayList<String>();
		listA.add("a");
		listA.add("b");
		
		ArrayList<String> listB = (ArrayList)listA.clone();
		listB.add("c");
		
		for (String str : listA) {
			System.out.println(str);
		}
	}
```

 结果：

  a

  b

 结论：预定义集合类型,元素为预定义非集合类型,执行clone()方法 -- 深拷贝

 

2. 元素为预定义集合类型

```
	private void testClone() {
		ArrayList<HashMap<Integer, String>> listA = new ArrayList<HashMap<Integer, String>>();
		
		HashMap<Integer, String> mapOne = new HashMap<Integer, String>();
		mapOne.put(1, "One");
		listA.add(mapOne);
		
		HashMap<Integer, String> mapTwo = new HashMap<Integer, String>();
		mapTwo.put(2, "Two");
		listA.add(mapTwo);
		
		ArrayList<HashMap<Integer, String>> listB = (ArrayList)listA.clone();
		HashMap<Integer, String> mapThree = new HashMap<Integer, String>();
		mapThree.put(3, "mapThree");
		listB.add(mapThree);
		
		System.out.println("--> listB 增加一个新元素, 打印listA:");
		for (HashMap<Integer, String> map : listA) {
			System.out.println("\t" + map.toString());
		}
		
		HashMap<Integer, String> mapOneOld = listB.get(0);
		mapOneOld.put(1, "New One");
		System.out.println("--> listB 修改一个旧元素值, 打印listA:");
		for (HashMap<Integer, String> map : listA) {
			System.out.println("\t" + map.toString());
		}
	}
```

 结果：

```
--> listB 增加一个新元素, 打印listA:
	{1=One}
	{2=Two}
--> listB 修改一个旧元素值, 打印listA:
	{1=New One}
	{2=Two}
```

 结论：预定义集合类型,元素为预定义集合类型,执行clone()方法 -- 实际是逐个调用每个元素的operator=方法，即这时的clone()方法其实为浅拷贝。

 

3. 元素为预定义集合类型，通过遍历元素的clone方法实现深拷贝

```
	private void testClone() {
		ArrayList<HashMap<Integer, String>> listA = new ArrayList<HashMap<Integer, String>>();
		
		HashMap<Integer, String> mapOne = new HashMap<Integer, String>();
		mapOne.put(1, "One");
		listA.add(mapOne);
		
		HashMap<Integer, String> mapTwo = new HashMap<Integer, String>();
		mapTwo.put(2, "Two");
		listA.add(mapTwo);
		
		ArrayList<HashMap<Integer, String>> listB = new ArrayList<HashMap<Integer, String>>();
		for (HashMap<Integer, String> map : listA) {
			listB.add((HashMap<Integer, String>)map.clone());
		}
		
		HashMap<Integer, String> mapThree = new HashMap<Integer, String>();
		mapThree.put(3, "mapThree");
		listB.add(mapThree);
		
		System.out.println("--> listB 增加一个新元素, 打印listA:");
		for (HashMap<Integer, String> map : listA) {
			System.out.println("\t" + map.toString());
		}
		
		HashMap<Integer, String> mapOneOld = listB.get(0);
		mapOneOld.put(1, "New One");
		System.out.println("--> listB 修改一个旧元素值, 打印listA:");
		for (HashMap<Integer, String> map : listA) {
			System.out.println("\t" + map.toString());
		}
	}
```

 结果：

```
--> listB 增加一个新元素, 打印listA:
	{1=One}
	{2=Two}
--> listB 修改一个旧元素值, 打印listA:
	{1=One}
	{2=Two}
```