日期:2014-05-18 浏览次数:20731 次
import java.io.ObjectInputStream; import java.io.ByteArrayInputStream; import java.io.ObjectOutputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; public class Test { public static Object deepClone(Object target) { Object retValue = null; try { ByteArrayOutputStream bo = new ByteArrayOutputStream(); ObjectOutputStream os = new ObjectOutputStream(bo); os.writeObject(target); ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(bo.toByteArray())); retValue = is.readObject(); } catch(IOException e){ e.printStackTrace(); } catch(ClassNotFoundException e){ e.printStackTrace(); } return retValue; } public static void main(String[] args) throws Exception { java.util.ArrayList list = new java.util.ArrayList(); list.add(1); System.out.println(deepClone(list)); list.add(2); System.out.println(list); } }