I was trying to find (using a code listed below) a simple solution for copying all the objects which are stored in first array, to second array, with changing paralelly the index of objects in second array + 1, so the object[0] in first array would be equal to object[1] in second one, and the last object[9] in the first one would be equal to the object[0] in second.
While trying to start code which I've written I've received message, which stated that "Destination array was not long enough Check destIndex and length, and the array's lower bounds", I'm just starting with arrays part at c#, so I'd be very gratefull for any quidance.
static void Main(string[] args)
{
int[] first = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
int[] second = new int[first.Length];
Array.Copy(first, 0, second, 1, 10);
foreach (int x in second)
{
Console.WriteLine("{0}", x);
}
Console.ReadKey();
}
}
Instead of Array.Copy
and foreach
you can do this:
for (int i = 0; i < first.Length; i++)
second[i == first.Length - 1 ? 0 : i + 1] = first[i];
It just goes from i = 0
to i = first.Length-1
, copying the element at that index in first
to that index plus 1 in second
.