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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/733243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:03:12  来源:igfitidea点击:

How to copy part of an array to another array in C#?

c#arrays

提问by SyncMaster

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

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

Consider I'm having

考虑我有

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

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

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

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.

就像我将开始索引设置为 1 并将结束索引设置为 3 一样,元素 2、3、4 应该被复制到新数组中。

采纳答案by Marc Gravell

int[] b = new int[3];
Array.Copy(a, 1, b, 0, 3);
  • a = source array
  • 1 = start index in source array
  • b = destination array
  • 0 = start index in destination array
  • 3 = elements to copy
  • a = 源数组
  • 1 = 源数组中的起始索引
  • b = 目标数组
  • 0 = 目标数组中的起始索引
  • 3 = 要复制的元素

回答by Pontus Gagge

See this question. LINQ Take() and Skip() are the most popular answers, as well as Array.CopyTo().

看到这个问题。LINQ Take() 和 Skip() 是最受欢迎的答案,还有 Array.CopyTo()。

A purportedly faster extension method is described here.

此处描述了一种据称更快的扩展方法

回答by Appurist - Paul W

Note: I found this question looking for one of the steps in the answer to how to resizean existing array.

注意:我发现这个问题是在寻找如何调整现有数组大小的答案中的步骤之一。

So I thought I would add that information here, in case anyone else was searching for how to do a ranged copy as a partial answer to the question of resizing an array.

所以我想我会在这里添加这些信息,以防其他人正在寻找如何进行范围复制作为调整数组大小问题的部分答案。

For anyone else finding this question looking for the same thing I was, it is very simple:

对于发现这个问题的其他人正在寻找与我相同的东西,这非常简单:

Array.Resize<T>(ref arrayVariable, newSize);

where Tis the type, i.e. where arrayVariable is declared:

其中T是类型,即声明 arrayVariable 的地方:

T[] arrayVariable;

That method handles null checks, as well as newSize==oldSize having no effect, and of course silently handles the case where one of the arrays is longer than the other.

该方法处理空值检查,以及 newSize==oldSize 无效,并且当然会静默处理其中一个数组比另一个长的情况。

See the MSDN articlefor more.

有关更多信息,请参阅MSDN 文章

回答by bajran

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

int [] b= new int[a.length]; //New Array and the size of a which is 4

Array.Copy(a,b,a.length);

Where Array is class having method Copy, which copies the element of a array to b array.

其中 Array 是具有方法 Copy 的类,它将数组的元素复制到 b 数组。

While copying from one array to another array, you have to provide same data type to another array of which you are copying.

从一个数组复制到另一个数组时,您必须为要复制的另一个数组提供相同的数据类型。

回答by Hameed Syed

In case if you want to implement your own Array.Copymethod.

如果您想实现自己的Array.Copy方法。

Static method which is of generic type.

泛型类型的静态方法。

 static void MyCopy<T>(T[] sourceArray, long sourceIndex, T[] destinationArray, long destinationIndex, long copyNoOfElements)
 {
  long totaltraversal = sourceIndex + copyNoOfElements;
  long sourceArrayLength = sourceArray.Length;

  //to check all array's length and its indices properties before copying
  CheckBoundaries(sourceArray, sourceIndex, destinationArray, copyNoOfElements, sourceArrayLength);
   for (long i = sourceIndex; i < totaltraversal; i++)
     {
      destinationArray[destinationIndex++] = sourceArray[i]; 
     } 
  }

Boundary method implementation.

边界方法实现。

private static void CheckBoundaries<T>(T[] sourceArray, long sourceIndex, T[] destinationArray, long copyNoOfElements, long sourceArrayLength)
        {
            if (sourceIndex >= sourceArray.Length)
            {
                throw new IndexOutOfRangeException();
            }
            if (copyNoOfElements > sourceArrayLength)
            {
                throw new IndexOutOfRangeException();
            }
            if (destinationArray.Length < copyNoOfElements)
            {
                throw new IndexOutOfRangeException();
            }
        }