Arrays.copyOf() 與 System.arraycopy()

System.arraycopy

首先觀察 System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length) 的實(shí)現(xiàn)方式:

public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
  • src - 源數(shù)組。
  • srcPos - 源數(shù)組中的起始位置。
  • dest - 目標(biāo)數(shù)組。
  • destPos - 目標(biāo)數(shù)據(jù)中的起始位置。
  • length - 要復(fù)制的數(shù)組元素的數(shù)量。

該方法是用了 native 關(guān)鍵字,調(diào)用的為 C++ 編寫的底層函數(shù),可見其為 JDK 中的底層函數(shù)。
System.arraycopy 是將元素復(fù)制到一個(gè)已經(jīng)存在的數(shù)組。

Arrays.copyOf

// 非基本類型 
public static <T> T[] copyOf(T[] original, int newLength) {  
    return (T[]) copyOf(original, newLength, original.getClass());  
}  

public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {    
    T[] copy = ((Object)newType == (Object)Object[].class)    
        ? (T[]) new Object[newLength]    
        : (T[]) Array.newInstance(newType.getComponentType(), newLength);    
    System.arraycopy(original, 0, copy, 0,    
                     Math.min(original.length, newLength));    
    return copy;    
}    

// 基本數(shù)據(jù)類型:int、boolean、char、double 等
public static int[] copyOf(int[] original, int newLength) {    
    int[] copy = new int[newLength];    
    System.arraycopy(original, 0, copy, 0,    
                     Math.min(original.length, newLength));    
    return copy;    
}   

Arrays.copyOf 是在其內(nèi)部創(chuàng)建了一個(gè)相同類型的新數(shù)組,然后調(diào)用 System.arraycopy() 復(fù)制元素,最后返回出去的是這個(gè)新數(shù)組。

Arrays.copyOf 是淺拷貝

import java.util.Arrays;

class Person {
    Person() {}
    Person(String name) {
        this.name = name;
    }

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

public class ArrayCopyOfTest {

    public static void main(String[] args) {
        Person[] persons = new Person[3];
        for (int i = 0; i < 3; i ++) {
            persons[i] = new Person("name_" + i);
        }
        Person[] copyPersons = Arrays.copyOf(persons, 3);

        copyPersons[0].setName("bnb");

        for (int i = 0; i < 3; i ++) {
            System.out.println(copyPersons[i].getName());
            // bnb name_1 name_2
        }
        for (int i = 0; i < 3; i ++) {
            System.out.println(persons[i].getName());
            // bnb name_1 name_2
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 區(qū)別:System.arraycopy()必須先建立一個(gè)數(shù)組對(duì)象,而Arrays.copyOf()返回一個(gè)數(shù)組對(duì)象...
    swiftwen閱讀 1,264評(píng)論 1 2
  • 第五章 數(shù)組 數(shù)組是一個(gè)基礎(chǔ)的數(shù)據(jù)結(jié)構(gòu),它用來存儲(chǔ)一組相同類型的元素的集合。數(shù)組非常有用,例如Java提供的集合...
    光劍書架上的書閱讀 612評(píng)論 0 6
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類相關(guān)的語(yǔ)法,內(nèi)部類的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚_t_閱讀 34,834評(píng)論 18 399
  • Arrays.copyOf方法用于數(shù)組復(fù)制 使用java.util.Arrays類的copyOf方法可實(shí)現(xiàn)數(shù)組的復(fù)...
    黑夜的眸閱讀 1,179評(píng)論 0 0
  • 晚上給媽媽打電話,媽媽說爸爸走了去了鄭州。過年的時(shí)候爸爸就說過年后要去鄭州,他沒走之前我沒啥感覺,等今天知道他已經(jīng)...
    明初的日記本閱讀 367評(píng)論 1 0

友情鏈接更多精彩內(nèi)容