<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Java List復制:淺拷貝與深拷貝方法及區(qū)別

          共 2338字,需瀏覽 5分鐘

           ·

          2021-12-23 01:08

          點擊上方“程序員大白”,選擇“星標”公眾號

          重磅干貨,第一時間送達

          List淺拷貝

          眾所周知,list本質上是數(shù)組,而數(shù)組的是以地址的形式進行存儲。

          如上圖將list A淺拷貝給list B,由于進行的是淺拷貝,所以直接將A的內容復制給了B,java中相同內容的數(shù)組指向同一地址,即進行淺拷貝后A與B指向同一地址。造成的后果就是,改變B的同時也會改變A,因為改變B就是改變B所指向地址的內容,由于A也指向同一地址,所以A與B一起改變。

          幾種淺拷貝

          1、遍歷循環(huán)復制

          List?destList=new?ArrayList(srcList.size());??
          for(Person?p?:?srcList){??
          ????destList.add(p);??
          }??

          2、使用List實現(xiàn)類的構造方法

          List?destList=new?ArrayList(srcList);??

          3、使用list.addAll()方法

          List?destList=new?ArrayList();??
          destList.addAll(srcList);??

          4、使用System.arraycopy()方法

          Person[]?srcPersons=srcList.toArray(new?Person[0]);??
          Person[]?destPersons=new?Person[srcPersons.length];??
          System.arraycopy(srcPersons,?0,?destPersons,?0,?srcPersons.length);??

          測試及結果

          printList(destList);?//打印未改變B之前的A?
          srcList.get(0).setAge(100);//改變B??
          printList(destList);?//打印改變B后的A

          //打印結果
          123-->20??
          ABC-->21??
          abc-->22??
          123-->100??
          ABC-->21??
          abc-->22??

          List深拷貝

          如圖,深拷貝就是將A復制給B的同時,給B創(chuàng)建新的地址,再將地址A的內容傳遞到地址B。ListA與ListB內容一致,但是由于所指向的地址不同,所以改變相互不受影響。

          深拷貝的方法

          1.使用序列化方法

          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);??
          ????@SuppressWarnings("unchecked")??
          ????List?dest?=?(List)?in.readObject();??
          ????return?dest;??
          }??

          List?destList=deepCopy(srcList);??//調用該方法

          2.clone方法

          public?class?A?implements?Cloneable?{???
          ????public?String?name[];???

          ????public?A(){???
          ????????name=new?String[2];???
          ????}???

          ????public?Object?clone()?{???
          ????????A?o?=?null;???
          ????????try?{???
          ????????????o?=?(A)?super.clone();???
          ????????}?catch?(CloneNotSupportedException?e)?{???
          ????????????e.printStackTrace();???
          ????????}???
          ????????return?o;???
          ????}???
          }??
          for(int?i=0;icopy.add((A)src.get(i).clone());
          }

          Java對對象和基本的數(shù)據(jù)類型的處理是不一樣的。在Java中用對象的作為入口參數(shù)的傳遞則缺省為”引用傳遞”,也就是說僅僅傳遞了對象的一個”引用”,這個”引用”的概念同C語言中的指針引用是一樣的。當函數(shù)體內部對輸入變量改變時,實質上就是在對這個對象的直接操作。

          除了在函數(shù)傳值的時候是”引用傳遞”,在任何用”=”向對象變量賦值的時候都是”引用傳遞”。

          測試及結果

          printList(destList);?//打印未改變B之前的A?
          srcList.get(0).setAge(100);//改變B??
          printList(destList);?//打印改變B后的A

          123-->20??
          ABC-->21??
          abc-->22??
          123-->20??
          ABC-->21??
          abc-->22??

          在淺復制的情況下,源數(shù)據(jù)被修改破壞之后,使用相同引用指向該數(shù)據(jù)的目標集合中的對應元素也就發(fā)生了相同的變化。因此,在需求要求必須深復制的情況下,要是使用上面提到的方法,請確保List中的T類對象是不易被外部修改和破壞的。

          來源:blog.csdn.net/demonliuhui/article/

          details/54572908


          13個你一定要知道的PyTorch特性

          解讀:為什么要做特征歸一化/標準化?

          一文搞懂 PyTorch 內部機制

          張一鳴:每個逆襲的年輕人,都具備的底層能力




          ,,西,,,[]!


          瀏覽 67
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  壁特壁视频免费观看 | 精品一区无码 | 手机免费看国产黄色电影 | 大鸡巴操逼网 | 亚洲国产婷婷 |