<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的淺拷貝與深拷貝!

          共 4306字,需瀏覽 9分鐘

           ·

          2021-05-18 17:57

          面試官:說是Java中List的淺拷貝與深拷貝!

          最近在面試很多 5 年左右的程序員,問到Java中List的淺拷貝與深拷貝,很少有人答對(duì)。今天我們一起來聊聊這個(gè)話題!

          眾所周知,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 就是改變 B 所指向地址的內(nèi)容,由于 A也指向同一地址,所以 A 與 B 一起改變。

          幾種淺拷貝

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

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

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

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

          使用list.addAll()方法

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

          使用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)容一致,但是由于所指向的地址不同,所以改變相互不受影響。

          深拷貝的方法

          下面聊聊深拷貝的幾種方法。

          使用序列化方法

          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)用該方法

          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ù)的傳遞則缺省為”引用傳遞”,也就是說僅僅傳遞了對(duì)象的一個(gè)”引用”,這個(gè)”引用”的概念同 C 語言中的指針引用是一樣的。當(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ì)象是不易被外部修改和破壞的。

          瀏覽 30
          點(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>
                  俺去官网一区 | 无码操操 | 欧美草比 | 欧美日韩国产中文精品字幕自在 | 91美女被艹 |