I am trying to move a number of elements in an array to the end using C#.
I have an Array (in my case a char-array), and a integer z
. Now I want to move z
chars to the end of another array, the other chars should move to the beginning of the array.
So if the first array is {'H','E','L','L','O'}
and z = 3
, the new array should be {'L','O','H','E','L'}
.
I hope somebody can help me.
Best attempt:
static char[] rotate(char[] c, int z)
{
char[] nc = new char[c.Length];
for (int i = z; i < c.Length; i++)
{
nc[i - z] = c[i];
}
for (int i = 0; i < z; i++)
{
nc[i + z] = c[i];
}
return nc;
}
question from:
https://stackoverflow.com/questions/65868794/how-to-rotate-an-array-by-moving-n-elements-from-end-to-start 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…