163

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 a it should get copied to another array.

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.

bluish
  • 26,356
  • 27
  • 122
  • 180
SyncMaster
  • 9,754
  • 34
  • 94
  • 137

6 Answers6

304
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
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 9
    With named parameters that are available now, you would not need to document any of the 5 parameters. – Hamish Grubijan Aug 06 '12 at 16:47
  • 12
    @Hamish well, maybe. Personally I wouldn't add explicit names unless it made the code significantly clearer, and I'm not sure (in this case) that the parameter names *by themselves* would achieve that. – Marc Gravell Aug 06 '12 at 17:57
19

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

A purportedly faster extension method is described here.

Community
  • 1
  • 1
Pontus Gagge
  • 17,166
  • 1
  • 38
  • 51
6
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.

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

JJJ
  • 32,902
  • 20
  • 89
  • 102
bajran
  • 1,433
  • 14
  • 23
3

In C# 8+ you can use ranges.

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

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/ranges

JasonC
  • 139
  • 8
1

Note: I found this question looking for one of the steps in the answer to how to resize an 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 T is the type, i.e. where arrayVariable is declared:

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.

See the MSDN article for more.

Appurist - Paul W
  • 1,291
  • 14
  • 22
0

In case if you want to implement your own Array.Copy method.

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();
            }
        }
Hameed Syed
  • 3,939
  • 2
  • 21
  • 31