Do you mean the following case?/***************************/ l1.add(map) l2=(LinkedList) l1.clone(); ... //map's content changed here //both l1 and l2 are changed /****************************/If so, you have to iterate the contents of l1, and add the clone of each element into l2.
l1.add("1231312");
l1.add("fdffd");LinkedList l2=(LinkedList) l1.clone();
System.out.println(l1==l2);
l1.add("fffff");System.out.println(l1);
System.out.println(l2);
l1.add(map)
l2=(LinkedList) l1.clone();
...
//map's content changed here
//both l1 and l2 are changed
/****************************/If so, you have to iterate the contents of l1, and add the clone of each element into l2.
while(i.hasNext()){
l2.add(((Map) i.next()).clone());
}
在JAVA中,能够在内存中产生新的对象有三种方式:
(1) new
(2) clone
(3) 反序列化楼主你自己看看应该怎么做呢?
<<
序列化:
对象 ---> 字节流反序列化:
字节流 ---> 对象
>>
意思理解了
用JAVA怎样表示呢?
<<
public class TestSerialization extends TestCase {
public TestSerialization(String name) {
super(name);
} public void testSerialization() throws Exception {
Data d1 = new Data();
d1.forTest = 99; ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(d1);
oos.close(); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
Data d2 = (Data) ois.readObject();
ois.close(); assertNotNull(d2);
assertNotSame(d1, d2);
assertEquals(d1.forTest, d2.forTest);
}
}class Data implements Serializable {
int forTest;
}
>>
这样实现对吧?
LinkedList list = new LinkedList();
HashMap map = new HashMap();
map.put("a","1");
list.add(map);LindedList cloneList = new LinkedList();
HashMap map1=new HashMap();
map1=(HashMap)map.clone();
cloneList.add(map1);
以后有问题直接找你行么?
小女子这相礼过去了:)
[email protected]