<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>

          List復(fù)制:深拷貝和淺拷貝用法及區(qū)別

          共 4455字,需瀏覽 9分鐘

           ·

          2021-08-22 01:15

          作者:DeMonnnnnn

            blog.csdn.net/demonliuhui/article/details/54572908

          List淺拷貝

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

          如上圖將list A淺拷貝給list B,由于進(jìn)行的是淺拷貝,所以直接將A的內(nèi)容復(fù)制給了B,java中相同內(nèi)容的數(shù)組指向同一地址,即進(jìn)行淺拷貝后A與B指向同一地址。

          造成的后果就是,改變B的同時(shí)也會(huì)改變A,因?yàn)楦淖傿就是改變B所指向地址的內(nèi)容,由于A也指向同一地址,所以A與B一起改變。

          幾種淺拷貝

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

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

          2、使用List實(shí)現(xiàn)類的構(gòu)造方法

          List<Person> destList=new ArrayList<Person>(srcList);  

          3、使用list.addAll()方法

          List<Person> destList=new ArrayList<Person>();  
          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);  

          測(cè)試及結(jié)果

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

          //打印結(jié)果
          123-->20  
          ABC-->21  
          abc-->22  
          123-->100  
          ABC-->21  
          abc-->22  

          List深拷貝

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

          深拷貝的方法

          1.使用序列化方法

          public static <T> List<T> deepCopy(List<T> 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<T> dest = (List<T>) in.readObject();  
              return dest;  
          }  

          List<Person> destList=deepCopy(srcList);  //調(diào)用該方法

          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;i<n;i+=){
          copy.add((A)src.get(i).clone());
          }

          Java對(duì)對(duì)象和基本的數(shù)據(jù)類型的處理是不一樣的。在Java中用對(duì)象的作為入口參數(shù)的傳遞則缺省為”引用傳遞”,也就是說(shuō)僅僅傳遞了對(duì)象的一個(gè)”引用”,這個(gè)”引用”的概念同C語(yǔ)言中的指針引用是一樣的。

          當(dāng)函數(shù)體內(nèi)部對(duì)輸入變量改變時(shí),實(shí)質(zhì)上就是在對(duì)這個(gè)對(duì)象的直接操作。除了在函數(shù)傳值的時(shí)候是”引用傳遞”,在任何用”=”向?qū)ο笞兞抠x值的時(shí)候都是”引用傳遞”。

          測(cè)試及結(jié)果

          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  

          在淺復(fù)制的情況下,源數(shù)據(jù)被修改破壞之后,使用相同引用指向該數(shù)據(jù)的目標(biāo)集合中的對(duì)應(yīng)元素也就發(fā)生了相同的變化。

          因此,在需求要求必須深復(fù)制的情況下,要是使用上面提到的方法,請(qǐng)確保List中的T類對(duì)象是不易被外部修改和破壞的。

          參考

          • http://blog.csdn.net/lian_1988/article/details/45970927
          • http://blog.sina.com.cn/s/blog_605f78830102uy6x.html

          分享一下我寫(xiě)的《10萬(wàn)字Springboot經(jīng)典學(xué)習(xí)筆記》中,點(diǎn)擊下面小卡片,進(jìn)入【Java禿頭哥】,回復(fù):筆記,即可免費(fèi)獲取。

          點(diǎn)贊是最大的支持 


          瀏覽 53
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <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>
                  夜拍拍拍拍 | 操逼大香蕉 | 成人自拍偷拍视频在线 | 777国产偷窥盗摄精品 | 肏屄视频小说 |