50,523
社区成员
发帖
与我相关
我的任务
分享
Map map1 = new HashMap();
map1.put("a" , 1);
map2.put("b" , 2);
Map map2 = new HashMap(map1);
map2.put("c" , 3);
System.out.println(map1);
System.out.println(map2);
Console :
{b=2, a=1}
{ b=2, c=3, a=1}
public static void main(String args[]) {
HashMap map1 = null;
HashMap map2 = new HashMap();
map2.put("1", "Hello");map2.put("2", "World");
map1 =(HashMap)map2.clone();
System.out.println(map1);
System.out.println(map2);
map1.put("1",(String)map1.get("1") + "World");
System.out.println(map1);
System.out.println(map2);
}
HashMap map1 = new HashMap();
HashMap map2 = new HashMap();
for(Iterator it = map1.keySet().iterator() ; it.hasNext();){
String key = it.next().toString();
map2.put(key, map1.get(key));
}
public class Dbclass implements Cloneable {
public String name;
public Object clone() {
Dbclass ta = null;
try {
ta = (Dbclass) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return ta;
}
public static void main(String args[]){
Dbclass ta1 = new Dbclass();
Dbclass ta2 = new Dbclass();
ta1.name = "ta1";
ta2 = (Dbclass)ta1.clone();//这个,楼主你写clone()不使用?
ta2.name = "ta2";
System.out.println("ta1:" + ta1.name);
System.out.println("ta2:" + ta2.name);
}
}