Array.ConstrainedCopy() 方法


Array.ConstrainedCopy() 方法用於從陣列中複製一系列元素,它從指定源索引開始,將它們貼上到另一個,從指定目標索引開始。

語法

public static void ConstrainedCopy (Array sourceArr, int sourceIndex, Array destinationArr, int destinationIndex, int length);

此處,

  • sourceArr - 包含要複製的資料的陣列。

  • sourceIndex - 整數,表示 sourceArr 中開始複製的索引。

  • destinationArr - 接收資料的陣列。

  • destinationIndex - 整數,表示 destinationArr 中開始儲存的索引。

  • len - 整數,表示要複製的元素數量。

現在我們提供一個示例來實現 Array.ConstrainedCopy() 方法 -

示例

using System;
public class Demo{
   public static void Main(){
      int[] arrDest = new int[10];
      Console.WriteLine("Array elements...");
      int[] arrSrc = { 20, 50, 100, 150, 200, 300, 400};
      for (int i = 0; i < arrSrc.Length; i++){
         Console.Write("{0} ", arrSrc[i]);
      }
      Console.WriteLine();
      Array.ConstrainedCopy(arrSrc, 3, arrDest, 0, 4);
      Console.WriteLine("Destination Array: ");
      for (int i = 0; i < arrDest.Length; i++){
         Console.Write("{0} ", arrDest[i]);
      }
   }
}

輸出

這將產生以下輸出 -

Array elements...
20 50 100 150 200 300 400
Destination Array:
150 200 300 400 0 0 0 0 0 0

更新於: 2019 年 11 月 4 日

234 次瀏覽

開始您的 職業

完成課程取得認證

立即開始
廣告