157

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
  • 24,718
  • 26
  • 114
  • 174
SyncMaster
  • 9,206
  • 31
  • 89
  • 129

6 Answers6

294
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
  • 976,458
  • 251
  • 2,474
  • 2,830
  • 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
  • 11
    @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
  • 16,973
  • 1
  • 37
  • 50
5
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,246
  • 20
  • 88
  • 102
bajran
  • 1,341
  • 13
  • 22
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
  • 983
  • 11
  • 19
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,531
  • 2
  • 18
  • 27
0

In C# 8+ you can use ranges.

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

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

JasonC
  • 21
  • 4