當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java System.arraycopy()用法及代碼示例


java.lang.System類為標準輸入和輸出,加載文件和庫或訪問外部定義的屬性提供了有用的方法。 java.lang.System.arraycopy()方法將源數組從特定的起始位置複製到目標位置(從所提到的位置)。要複製的參數數量由len參數決定。 source_Position到source_Position +長度-1的組件從destination_Position到destination_Position +長度-1複製到目標數組

類聲明

public final class System
   extends Object

arraycopy
用法:


public static void arraycopy(Object source_arr, int sourcePos,
                            Object dest_arr, int destPos, int len)
參數:
source_arr:array to be copied from
sourcePos:starting position in source array from where to copy
dest_arr:array to be copied in
destPos:starting position in destination array, where to copy in
len:total no. of components to be copied.

實作

// Java program explaining System class method - arraycopy() 
import java.lang.*; 
public class NewClass 
{ 
    public static void main(String[] args) 
    { 
        int s[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; 
        int d[] = { 15, 25, 35, 45, 55, 65, 75, 85, 95, 105}; 
  
        int source_arr[], sourcePos, dest_arr[], destPos, len; 
        source_arr = s; 
        sourcePos = 3; 
        dest_arr = d; 
        destPos = 5; 
        len = 4; 
  
        // Print elements of source 
        System.out.print("source_array:"); 
        for (int i = 0; i < s.length; i++) 
            System.out.print(s[i] + " "); 
        System.out.println(""); 
  
        System.out.println("sourcePos:" + sourcePos); 
         
        // Print elements of source 
        System.out.print("dest_array:"); 
        for (int i = 0; i < d.length; i++) 
            System.out.print(d[i] + " "); 
        System.out.println(""); 
         
        System.out.println("destPos:" + destPos); 
         
        System.out.println("len:" + len); 
         
        // Use of arraycopy() method 
        System.arraycopy(source_arr, sourcePos, dest_arr,  
                                            destPos, len); 
         
        // Print elements of destination after 
        System.out.print("final dest_array:"); 
        for (int i = 0; i < d.length; i++) 
            System.out.print(d[i] + " "); 
    } 
} 

輸出:

source_array:10 20 30 40 50 60 70 80 90 100 
sourcePos:3
dest_array:15 25 35 45 55 65 75 85 95 105 
destPos:5
len:4
final dest_array:15 25 35 45 55 40 50 60 70 105 


相關用法


注:本文由純淨天空篩選整理自 System.arraycopy() in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。