利用串行化实现ArrayList深拷贝
??? /**
??? ?* 實現ArrayList的深復制:將ArrayList的原來的物件進行序列化,然後通過反序列化讀取出來
??? ?* @param src
??? ?* @return
??? ?* @throws IOException
??? ?* @throws ClassNotFoundException
??? ?* @createTime 2009-08-15
??? ?*/
??? public static List<?> deepCopy(List<?> src) throws IOException, ClassNotFoundException{??
??? ??? ??? ByteArrayOutputStream byteOut = new ByteArrayOutputStream();??
??? ??? ??? ObjectOutputStream out = new ObjectOutputStream(byteOut);??
??? ??? ??? out.writeObject(src);??
??? ??? ??
??? ??? ??? ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());??
??? ??? ??? ObjectInputStream in =new ObjectInputStream(byteIn);??
??? ??? ??? List<?> dest = (List<?>)in.readObject();??
??? ??? ??? return dest;??
??? ??? }
總結
以上是生活随笔為你收集整理的利用串行化实现ArrayList深拷贝的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA中浅复制与深复制
- 下一篇: EJB3.0 JPQL