我想将整个数组移动一位。示例:如果数组从 [0] 到 [19],我希望它从 [1] 到 [20],[0] 消失。
目前的做法(这是错误的):
shiftRight(operationsList.ToArray()));
public Operation3D[] shiftRight(Operation3D[] arr)
{
Operation3D[] demo = new Operation3D[arr.Length];
for (int i = 1; i < arr.Length; i++)
{
demo[i] = arr[i - 1];
}
demo[0] = arr[demo.Length - 1];
return demo;
}
最佳答案
由于调整大小,将元素向右移动将需要一个新数组。没有办法让索引 0
消失。它必须存在,但可以是 null
。
下面的代码将完成这个:
public Operation3D[] shiftRight(Operation3D[] arr)
{
Operation3D[] result = new Operation3D[arr.Length + 1];
Array.Copy(arr, 0, result, 1, arr.Length);
return result;
}
关于C# 将整个数组移动 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43374955/