如何在C#中将数组的一部分复制到另一个数组? [英] How to copy part of an array to another array in C#?

查看:33
本文介绍了如何在C#中将数组的一部分复制到另一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将数组的一部分复制到另一个数组?

How can I copy a part of an array to another array?

考虑一下我有

int[] a = {1,2,3,4,5};

现在,如果我给出数组 a 的起始索引和结束索引,它应该被复制到另一个数组.

Now if I give the start index and end index of the array a it should get copied to another array.

就像我给开始索引为 1 和结束索引为 3 一样,元素 2、3、4 应该被复制到新数组中.

Like if I give start index as 1 and end index as 3, the elements 2, 3, 4 should get copied in the new array.

推荐答案

int[] b = new int[3];
Array.Copy(a, 1, b, 0, 3);

  • a = 源数组
  • 1 = 源数组中的起始索引
  • b = 目标数组
  • 0 = 目标数组中的起始索引
  • 3 = 要复制的元素
  • 这篇关于如何在C#中将数组的一部分复制到另一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆