How to shift the start of an array in C#?

后端 未结 8 2009
渐次进展
渐次进展 2020-12-18 07:31

I\'m trying to reorganize an array based on the first occurrence of a value (thus simulating similar functionality to a circular array.)

For example, in the followin

8条回答
  •  我在风中等你
    2020-12-18 08:13

    C# answer: input : { 1, 2, 3, 5, 6, 7, 8 }; Output : { 8, 7 1, 2, 3, 5, 6};

    {
        static void Main(string[] args)
        {
            int[] array = { 1, 2, 3, 5, 6, 7, 8 };
            int index = 2;
            int[] tempArray = new int[array.Length];
            array.CopyTo(tempArray, 0);
    
            for (int i = 0; i < array.Length - index; i++)
            {
                array[index + i] = tempArray[i];
            }
    
            for (int i = 0; i < index; i++)
            {
                array[i] = tempArray[array.Length -1 - i];
            }            
    
    
        }
    }
    

提交回复
热议问题