如何复制数组的一部分到另外一个数组
答案:1 悬赏:30
解决时间 2021-02-11 22:02
- 提问者网友:心裂忍耐
- 2021-02-10 23:08
如何复制数组的一部分到另外一个数组
最佳答案
- 二级知识专家网友:啵啵桃汀
- 2021-02-11 00:39
用Array.Copy方法将数组或者数组的一部分复制到另个数组。Array.Copy是静态方法,有多个重载版本。其中常用的是:
1
2
3
4
5
6
public static void Copy(
Array sourceArray,
int sourceIndex,
Array destinationArray,
int destinationIndex,
int length);
各个参数含义如下
sourceArray —— 源数组
sourceIndex —— 表示 sourceArray 中复制开始处的索引
destinationArray —— 目标数组,它接收数据
destinationIndex —— 表示 destinationArray 中存储开始处的索引
length —— 要复制的元素数目。
用法举例如下:
(1)复制数组的一部分到另一个数组
1
2
3
4
int[] src = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
int[] dest = new int[4];
// 将数组 src 中元素 2,3,4,5 复制到 dest
Array.Copy(src, 1, dest, 0, 4);
(2)复制整个数组
1
2
3
4
int[] src = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
int[] dest = new int[src.Length];
// 将数组 src 所有元素复制到 dest
Array.Copy(src, 0, dest, 0, src.Length);
1
2
3
4
5
6
public static void Copy(
Array sourceArray,
int sourceIndex,
Array destinationArray,
int destinationIndex,
int length);
各个参数含义如下
sourceArray —— 源数组
sourceIndex —— 表示 sourceArray 中复制开始处的索引
destinationArray —— 目标数组,它接收数据
destinationIndex —— 表示 destinationArray 中存储开始处的索引
length —— 要复制的元素数目。
用法举例如下:
(1)复制数组的一部分到另一个数组
1
2
3
4
int[] src = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
int[] dest = new int[4];
// 将数组 src 中元素 2,3,4,5 复制到 dest
Array.Copy(src, 1, dest, 0, 4);
(2)复制整个数组
1
2
3
4
int[] src = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
int[] dest = new int[src.Length];
// 将数组 src 所有元素复制到 dest
Array.Copy(src, 0, dest, 0, src.Length);
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯