Java EnumMap clone()
clone() 方法用于将一个EnumMap的数据复制到另一个EnumMap。此方法创建EnumMap的浅表副本。
1 语法
public EnumMap<K, V> clone()
2 参数
无
3 返回值
返回EnumMap的浅表副本。
4 EnumMap clone()示例1
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* Java EnumMap.clone的例子
*/
import java.util.*;
public class Demo {
// create an enum
public enum Tutorial {
Java, Python, PHP, Android, AngularJS
};
public static void main(String[] args) {
//创建EnumMap
EnumMap<Tutorial, String> map1 =
new EnumMap<Tutorial, String> (Tutorial.class);
EnumMap<Tutorial, String> map2 =
new EnumMap<Tutorial, String> (Tutorial.class);
map1.put(Tutorial.Java,"1");
map1.put(Tutorial.Python,"2");
map1.put(Tutorial.PHP,"3");
map1.put(Tutorial.Android,"4");
map1.put(Tutorial.AngularJS,"5");
// 第一次打印map
System.out.println("clone前:");
System.out.println("map1:"+map1);
System.out.println("map2:"+map2);
// 调用clone方法
map2 = map1.clone();
//再次打印map
System.out.println("clone后:");
System.out.println("map1:"+map1);
System.out.println("map2:"+map2);
}
}
输出结果为:
clone前:
map1:{Java=1, Python=2, PHP=3, Android=4, AngularJS=5}
map2:{}
clone后:
map1:{Java=1, Python=2, PHP=3, Android=4, AngularJS=5}
map2:{Java=1, Python=2, PHP=3, Android=4, AngularJS=5}
5 EnumMap clone()示例2
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* Java EnumMap.clone的例子
*/
import java.util.*;
public class Demo {
//创建枚举
public enum Days {
Monday, Tuesday, Wednesday, Thursday
};
public static void main(String[] args) {
//创建EnumMap
EnumMap<Days, String> map1= new EnumMap<Days, String>(Days.class);
EnumMap<Days, String> map2= new EnumMap<Days, String>(Days.class);
map1.put(Days.Monday,"1");
map1.put(Days.Tuesday,"2");
map1.put(Days.Wednesday,"3");
map1.put(Days.Thursday,"4");
// 第一次打印Map
System.out.println("clone前:");
System.out.println("map1:"+map1);
System.out.println("map2:"+map2);
// 调用clone方法
map2 = map1.clone();
// 再次打印Map
System.out.println("clone后:");
System.out.println("map1:"+map1);
System.out.println("map2:"+map2);
}
}
输出结果为:
clone前:
map1:{Monday=1, Tuesday=2, Wednesday=3, Thursday=4}
map2:{}
clone后:
map1:{Monday=1, Tuesday=2, Wednesday=3, Thursday=4}
map2:{Monday=1, Tuesday=2, Wednesday=3, Thursday=4}
热门文章
优秀文章