C# 陣列複製
在 C# 中使用 array.copy 方法將一段陣列複製到另一個數組。
我們的原始陣列有 10 個元素 −
int [] n = new int[10]; /* n is an array of 10 integers */
我們的新陣列(將複製陣列 1 中的一段)有 5 個元素 −
int [] m = new int[5]; /* m is an array of 5 integers */
array. copy() 方法允許你新增源陣列和目標陣列。此外,請包括第一組陣列包含的第二組陣列部分的大小。
示例
你可以嘗試執行以下程式碼在 C# 中實現陣列複製 −
using System;
namespace ArrayApplication {
class MyArray {
static void Main(string[] args) {
int [] n = new int[10]; /* n is an array of 10 integers */
int [] m = new int[5]; /* m is an array of 5 integers */
for ( int i = 0; i < 10; i++ ) {
n[i] = i + 100;
}
Console.WriteLine("Original Array...");
foreach (int j in n ) {
int i = j-100;
Console.WriteLine("Element[{0}] = {1}", i, j);
}
Array.Copy(n, 0, m, 0, 5);
Console.WriteLine("New Array...");
foreach (int res in m) {
Console.WriteLine(res);
}
Console.ReadKey();
}
}
}輸出
Original Array... Element[0] = 100 Element[1] = 101 Element[2] = 102 Element[3] = 103 Element[4] = 104 Element[5] = 105 Element[6] = 106 Element[7] = 107 Element[8] = 108 Element[9] = 109 New Array... 100 101 102 103 104
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP