CodeGym /Java Blog /Toto sisi /Java System.arraycopy() 方法
John Squirrels
等級 41
San Francisco

Java System.arraycopy() 方法

在 Toto sisi 群組發布

什麼是 Java System.arraycopy() 方法?

java.lang.System.arraycopy() 方法將特定起始位置的源數組複製到給定索引處的目標。
此方法屬於java.lang.System類。它將指定長度的子數組的內容從給定的數組複製到另一個稱為目標數組的數組。源數組稱為 src ,目標數組稱為dest。元素的數量由len參數給出。

Java System.arraycopy()方法聲明

java.lang類中java.lang.System.arraycopy()方法的方法聲明如下:

public static void arraycopy(Object src, int srcIndex, Object dest, int destIndex, int len)

參數

以下是arraycopy方法的參數:
  1. src:它是源數組。

  2. srcIndex:它是源數組的起始索引。

  3. dest:它是目標數組。

  4. destIndex:它是目標數組的起始索引。

  5. len:它是需要從源數組複製到目標數組的元素數。

功能性

arraycopy方法從src複製數據,從srcIndexsrcIndex +( len - 1)元素,到destIndexdestIndex + ( len - 1) 個元素的 dest 數組。

返回值

arraycopy方法有一個void返回類型,這意味著它不返回任何東西

例子


public class Example {

    public static void main(String[] args) {

        String[] src = { "Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
        String[] dest = { "January", "February", "March", "April", "May", "June", "July", "August"};
  
        int srcIndex = 2;
        int destIndex = 3;
        int len = 2;
        
        //print number of elements that need to be copied 
        //from the source to the destination array  
        System.out.println("len : " + len);
        
        //print source index
        System.out.println("srcIndex : " + srcIndex);
  
        //print elements of the source array
        System.out.print("src : ");
        for (int i = 0; i < src.length; i++)
            System.out.print(src[i] + " ");
        System.out.println("");
  
        //print destination index
        System.out.println("destIndex : " + destIndex); 
        
        //print elements of the destination array
        System.out.print("dest : ");
        for (int i = 0; i < dest.length; i++)
            System.out.print(dest[i] + " ");
        System.out.println("");
                
        // Use of arraycopy() method
        System.arraycopy(src, srcIndex, dest, destIndex, len); 
        // this method copies the 'len' no of elements 
        // from the src array to the dest array using the srcIndex
        // and destIndex as reference points in both the arrays
        
                        
        // Print elements of destination after  
        System.out.print("final destination array : ");
        for (int i = 0; i < dest.length; i++)
            System.out.print(dest[i] + " ");
    }
}

輸出

len:2 srcIndex:2 src:週一,週二,週三,週四,週五,週六,週日,destIndex:3

改變參數的例子


// Example of changing parameters
         srcIndex = 4;
         destIndex = 5;
         len = 1;

輸出

len : 1 srcIndex : 4 src : Monday Tuesday Wednesday Thursday Friday Saturday Sunday destIndex : 5 dest : January February March April May June July July August 最終目的地數組:January February March April May Friday July August

解釋

在上面的代碼中,我們定義了兩個數組src[]dest[]。之後,我們定義了srcIndexdestIndex來定義兩個數組的引用位置。 在第一個示例中System.arraycopy方法將源數組src[]中的元素從索引2 開始並在索引 3 結束(因為len = 2)複製到目標數組,從索引 3 開始。然後輸出複製元素後的最終目標數組。 在第二個示例中System.arraycopy方法從源數組中復制 1 個元素(因為len = 1)src[],從索引 4 開始,到目標數組,從索引 5 開始,並在復制元素後輸出最終目標數組。

結論

到本文結束時,我們希望您已經熟悉了Java 中的java.lang.System.arraycopy方法。繼續練習以更深入地掌握這個概念。到那時,繼續成長,繼續發光!
留言
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION